Jump to content

Taxicab numbers: Difference between revisions

Added Julia language
m (→‎{{header|REXX}}: mode some cosmetic changes.)
(Added Julia language)
Line 1,617:
2005: 1676926719 ~ [63,1188] and [714,1095]
2006: 1677646971 ~ [99,1188] and [891,990]</lang>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Python}}
 
<lang julia>using DataStructures, IterTools
 
function findtaxinumbers(nmax::Integer)
cube2n = Dict{Int,Int}(x ^ 3 => x for x in 0:nmax)
sum2cubes = DefaultDict{Int,Set{NTuple{2,Int}}}(Set{NTuple{2,Int}})
for ((c1, _), (c2, _)) in product(cube2n, cube2n)
if c1 ≥ c2
push!(sum2cubes[c1 + c2], (cube2n[c1], cube2n[c2]))
end
end
 
taxied = collect((k, v) for (k, v) in sum2cubes if length(v) ≥ 2)
return sort!(taxied, by = first)
end
taxied = findtaxinumbers(1200)
 
for (ith, (cube, set)) in zip(1:25, taxied[1:25])
@printf "%2i: %7i = %s\n" ith cube join(set, ", ")
# println(ith, ": ", cube, " = ", join(set, ", "))
end
println("...")
for (ith, (cube, set)) in zip(2000:2006, taxied[2000:2006])
@printf "%-4i: %i = %s\n" ith cube join(set, ", ")
end
 
# version 2
function findtaxinumbers(nmax::Integer)
cubes, crev = collect(x ^ 3 for x in 1:nmax), Dict{Int,Int}()
for (x, x3) in enumerate(cubes)
crev[x3] = x
end
sums = collect(x + y for x in cubes for y in cubes if y < x)
sort!(sums)
 
idx = 0
for i in 2:(endof(sums) - 1)
if sums[i-1] != sums[i] && sums[i] == sums[i+1]
idx += 1
if 25 < idx < 2000 || idx > 2006 continue end
n, p = sums[i], NTuple{2,Int}[]
for x in cubes
n < 2x && break
if haskey(crev, n - x)
push!(p, (crev[x], crev[n - x]))
end
end
@printf "%4d: %10d" idx n
for x in p @printf(" = %4d ^ 3 + %4d ^ 3", x...) end
println()
end
end
end
 
findtaxinumbers(1200)</lang>
 
{{out}}
<pre> 1: 1729 = (12, 1), (10, 9)
2: 4104 = (16, 2), (15, 9)
3: 13832 = (24, 2), (20, 18)
4: 20683 = (27, 10), (24, 19)
5: 32832 = (32, 4), (30, 18)
6: 39312 = (33, 15), (34, 2)
7: 40033 = (34, 9), (33, 16)
8: 46683 = (30, 27), (36, 3)
9: 64232 = (36, 26), (39, 17)
10: 65728 = (33, 31), (40, 12)
11: 110656 = (48, 4), (40, 36)
12: 110808 = (48, 6), (45, 27)
13: 134379 = (43, 38), (51, 12)
14: 149389 = (50, 29), (53, 8)
15: 165464 = (54, 20), (48, 38)
16: 171288 = (54, 24), (55, 17)
17: 195841 = (57, 22), (58, 9)
18: 216027 = (59, 22), (60, 3)
19: 216125 = (60, 5), (50, 45)
20: 262656 = (64, 8), (60, 36)
21: 314496 = (66, 30), (68, 4)
22: 320264 = (66, 32), (68, 18)
23: 327763 = (67, 30), (58, 51)
24: 373464 = (60, 54), (72, 6)
25: 402597 = (69, 42), (61, 56)
...
2000: 1671816384 = (944, 940), (1168, 428)
2001: 1672470592 = (1124, 632), (1187, 29)
2002: 1673170856 = (1034, 828), (1164, 458)
2003: 1675045225 = (1081, 744), (1153, 522)
2004: 1675958167 = (1159, 492), (1096, 711)
2005: 1676926719 = (1188, 63), (1095, 714)
2006: 1677646971 = (1188, 99), (990, 891)
1: 1729 = 1 ^ 3 + 12 ^ 3 = 9 ^ 3 + 10 ^ 3
2: 4104 = 2 ^ 3 + 16 ^ 3 = 9 ^ 3 + 15 ^ 3
3: 13832 = 2 ^ 3 + 24 ^ 3 = 18 ^ 3 + 20 ^ 3
4: 20683 = 10 ^ 3 + 27 ^ 3 = 19 ^ 3 + 24 ^ 3
5: 32832 = 4 ^ 3 + 32 ^ 3 = 18 ^ 3 + 30 ^ 3
6: 39312 = 2 ^ 3 + 34 ^ 3 = 15 ^ 3 + 33 ^ 3
7: 40033 = 9 ^ 3 + 34 ^ 3 = 16 ^ 3 + 33 ^ 3
8: 46683 = 3 ^ 3 + 36 ^ 3 = 27 ^ 3 + 30 ^ 3
9: 64232 = 17 ^ 3 + 39 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= 63 ^ 3 + 1188 ^ 3 = 714 ^ 3 + 1095 ^ 3
2006: 1677646971 = 99 ^ 3 + 1188 ^ 3 = 891 ^ 3 + 990 ^ 3</pre>
 
=={{header|Kotlin}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.