Achilles numbers: Difference between revisions
Content added Content deleted
(bugfix (thanks for the clear report @Thundergnat)) |
(Added Wren) |
||
Line 149: | Line 149: | ||
8 digits: 7395 |
8 digits: 7395 |
||
9 digits: 24008</pre> |
9 digits: 24008</pre> |
||
=={{header|Wren}}== |
|||
{{libheader|Wren-seq}} |
|||
{{libheader|Wren-fmt}} |
|||
<lang ecmascript>import "./seq" for Lst |
|||
import "./fmt" for Fmt |
|||
var totient = Fn.new { |n| |
|||
var tot = n |
|||
var i = 2 |
|||
while (i*i <= n) { |
|||
if (n%i == 0) { |
|||
while(n%i == 0) n = (n/i).floor |
|||
tot = tot - (tot/i).floor |
|||
} |
|||
if (i == 2) i = 1 |
|||
i = i + 2 |
|||
} |
|||
if (n > 1) tot = tot - (tot/n).floor |
|||
return tot |
|||
} |
|||
var isPerfectPower = Fn.new { |n| |
|||
if (n == 1) return true |
|||
var x = 2 |
|||
while (x * x <= n) { |
|||
var y = 2 |
|||
var p = x.pow(y) |
|||
while (p > 0 && p <= n) { |
|||
if (p == n) return true |
|||
y = y + 1 |
|||
p = x.pow(y) |
|||
} |
|||
x = x + 1 |
|||
} |
|||
return false |
|||
} |
|||
var isPowerful = Fn.new { |n| |
|||
while (n % 2 == 0) { |
|||
var p = 0 |
|||
while (n % 2 == 0) { |
|||
n = (n/2).floor |
|||
p = p + 1 |
|||
} |
|||
if (p == 1) return false |
|||
} |
|||
var f = 3 |
|||
while (f * f <= n) { |
|||
var p = 0 |
|||
while (n % f == 0) { |
|||
n = (n/f).floor |
|||
p = p + 1 |
|||
} |
|||
if (p == 1) return false |
|||
f = f + 2 |
|||
} |
|||
return n == 1 |
|||
} |
|||
var isAchilles = Fn.new { |n| isPowerful.call(n) && !isPerfectPower.call(n) } |
|||
var isStrongAchilles = Fn.new { |n| |
|||
if (!isAchilles.call(n)) return false |
|||
var tot = totient.call(n) |
|||
return isAchilles.call(tot) |
|||
} |
|||
System.print("First 50 Achilles numbers:") |
|||
var achilles = [] |
|||
var count = 0 |
|||
var n = 2 |
|||
while (count < 50) { |
|||
if (isAchilles.call(n)) { |
|||
achilles.add(n) |
|||
count = count + 1 |
|||
} |
|||
n = n + 1 |
|||
} |
|||
for (chunk in Lst.chunks(achilles, 10)) Fmt.print("$4d", chunk) |
|||
System.print("\nFirst 30 strong Achilles numbers:") |
|||
var strongAchilles = [] |
|||
count = 0 |
|||
n = achilles[0] |
|||
while (count < 30) { |
|||
if (isStrongAchilles.call(n)) { |
|||
strongAchilles.add(n) |
|||
count = count + 1 |
|||
} |
|||
n = n + 1 |
|||
} |
|||
for (chunk in Lst.chunks(strongAchilles, 10)) Fmt.print("$5d", chunk) |
|||
System.print("\nNumber of Achilles numbers with:") |
|||
var pow = 10 |
|||
for (i in 2..7) { |
|||
var count = 0 |
|||
for (j in pow..pow*10-1) { |
|||
if (isAchilles.call(j)) count = count + 1 |
|||
} |
|||
System.print("%(i) digits: %(count)") |
|||
pow = pow * 10 |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
First 50 Achilles numbers: |
|||
72 108 200 288 392 432 500 648 675 800 |
|||
864 968 972 1125 1152 1323 1352 1372 1568 1800 |
|||
1944 2000 2312 2592 2700 2888 3087 3200 3267 3456 |
|||
3528 3872 3888 4000 4232 4500 4563 4608 5000 5292 |
|||
5324 5400 5408 5488 6075 6125 6272 6728 6912 7200 |
|||
First 30 strong Achilles numbers: |
|||
500 864 1944 2000 2592 3456 5000 10125 10368 12348 |
|||
12500 16875 19652 19773 30375 31104 32000 33275 37044 40500 |
|||
49392 50000 52488 55296 61731 64827 67500 69984 78608 80000 |
|||
Number of Achilles numbers with: |
|||
2 digits: 1 |
|||
3 digits: 12 |
|||
4 digits: 47 |
|||
5 digits: 192 |
|||
6 digits: 664 |
|||
7 digits: 2242 |
|||
</pre> |