Exponential digital sums: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: at the same time remark) |
|||
Line 28: | Line 28: | ||
* Find and show at least the first ten integers with their exponents, that satisfy this condition in three or more ways. |
* Find and show at least the first ten integers with their exponents, that satisfy this condition in three or more ways. |
||
=={{header|Julia}}== |
|||
<syntaxhighlight lang="julia">function equal_digitalsum_exponents(n::Integer) |
|||
equalpows = Int[] |
|||
if n > 1 |
|||
npow, misses = 2, 0 |
|||
while misses < n + 50 |
|||
dsum = sum(digits(BigInt(n) ^ npow)) |
|||
if npow > 10 && dsum > 2 * n |
|||
break # bail here for time contraints (see Wren example) |
|||
elseif dsum == n |
|||
push!(equalpows, npow) |
|||
else |
|||
misses += 1 |
|||
end |
|||
npow += 1 |
|||
end |
|||
end |
|||
return equalpows |
|||
end |
|||
function testdigitalequals(lim, wanted, multiswanted) |
|||
found1, found2, multis = 0, 0, Tuple{Int, Vector{Int}}[] |
|||
println("First $wanted integers that are equal to the digital sum of that integer raised to some power:") |
|||
for i in 1:lim |
|||
a = equal_digitalsum_exponents(i) |
|||
if !isempty(a) |
|||
found1 += 1 |
|||
if found1 <= wanted |
|||
print(rpad(i, 6), found1 % 10 == 0 ? "\n" : "") |
|||
end |
|||
if length(a) > 2 |
|||
found2 += 1 |
|||
push!(multis, (i, a)) |
|||
if found2 == multiswanted |
|||
println("\nFirst $multiswanted that satisfy that condition in three or more ways:") |
|||
for (n, v) in multis |
|||
println("$n: powers $v") |
|||
end |
|||
break |
|||
end |
|||
end |
|||
end |
|||
end |
|||
end |
|||
testdigitalequals(6000, 25, 30) |
|||
<syntaxhighlight>{{out}} |
|||
<pre> |
|||
First 25 integers that are equal to the digital sum of that integer raised to some power: |
|||
7 8 9 17 18 20 22 25 26 27 |
|||
28 31 34 35 36 40 43 45 46 53 |
|||
54 58 63 64 68 |
|||
First 30 that satisfy that condition in three or more ways: |
|||
18: powers [3, 6, 7] |
|||
54: powers [6, 8, 9] |
|||
90: powers [19, 20, 21, 22, 28] |
|||
107: powers [11, 13, 15] |
|||
181: powers [16, 18, 19, 20] |
|||
360: powers [45, 46, 49, 51] |
|||
370: powers [48, 54, 57, 59] |
|||
388: powers [32, 35, 36] |
|||
523: powers [39, 42, 44, 45] |
|||
603: powers [44, 47, 54] |
|||
667: powers [48, 54, 58] |
|||
793: powers [57, 60, 64] |
|||
1062: powers [72, 77, 81] |
|||
1134: powers [78, 80, 82, 86] |
|||
1359: powers [92, 98, 102] |
|||
1827: powers [121, 126, 131] |
|||
1828: powers [123, 127, 132] |
|||
2116: powers [140, 143, 147] |
|||
2330: powers [213, 215, 229] |
|||
2430: powers [217, 222, 223, 229, 230] |
|||
2557: powers [161, 166, 174] |
|||
2610: powers [228, 244, 246] |
|||
2656: powers [170, 172, 176] |
|||
2700: powers [406, 414, 420, 427] |
|||
2871: powers [177, 189, 190] |
|||
2934: powers [191, 193, 195] |
|||
3077: powers [187, 193, 199] |
|||
3222: powers [189, 202, 210] |
|||
3231: powers [203, 207, 209] |
|||
3448: powers [215, 221, 227] |
|||
</pre> |
|||