Jump to content

Combinations with repetitions/Square digit chain: Difference between revisions

(Added Perl example)
Line 210:
12024696404768024 numbers produce 1 and 87975303595231975 numbers produce 89
</pre>
 
=={{header|Julia}}==
<lang julia>using Combinatorics
 
function iterate(m::Integer)
while m != 1 && m != 89
s = 0
while m > 0 # compute sum of squares of digits
m, d = divrem(m, 10)
s += d ^ 2
end
m = s
end
return m
end
 
function uniquepermcount(a)
ret = factorial(length(a))
elems = unique(a)
for elem in elems
ret /= factorial(sum(x -> x == elem, a))
end
Int(ret)
end
 
function testitersquares(numdigits)
items = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
onecount, eightyninecount = 0, 0
for combo in with_replacement_combinations(items, numdigits)
if all(x -> x == 0, combo)
continue
end
pcount = uniquepermcount(combo)
if iterate(sum(combo)) == 89
eightyninecount += pcount
else
onecount += pcount
end
end
println("For k = $numdigits, in the range 1 to $("9" ^ numdigits),\n" *
"$onecount numbers produce 1 and $eightyninecount numbers produce 89.\n")
end
 
for i in [7, 8, 11, 14, 17]
testitersquares(i)
end
</lang>{{out}}
<pre>
For k = 2, in the range 1 to 99,
19 numbers produce 1 and 80 numbers produce 89.
 
For k = 7, in the range 1 to 9999999,
1418853 numbers produce 1 and 8581146 numbers produce 89.
 
For k = 8, in the range 1 to 99999999,
14255666 numbers produce 1 and 85744333 numbers produce 89.
 
For k = 11, in the range 1 to 99999999999,
15091199356 numbers produce 1 and 84908800643 numbers produce 89.
 
For k = 14, in the range 1 to 99999999999999,
13770853279684 numbers produce 1 and 86229146720315 numbers produce 89.
 
For k = 17, in the range 1 to 99999999999999999,
12024696404768024 numbers produce 1 and 87975303595231975 numbers produce 89.
</pre>
 
 
=={{header|Kotlin}}==
4,105

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.