Achilles numbers: Difference between revisions

→‎{{header|Wren}}: Tidied and moved totient function to Math library as it's used quite often.
(Added BASIC256)
(→‎{{header|Wren}}: Tidied and moved totient function to Math library as it's used quite often.)
Line 3,281:
===Version 1 (Brute force)===
This finds the number of 6 digit Achilles numbers in 2.5 seconds, 7 digits in 51 seconds but 8 digits needs a whopping 21 minutes!
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
Line 3,288:
var limit = 10.pow(maxDigits)
var c = Int.primeSieve(limit-1, false)
 
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|
Line 3,346 ⟶ 3,331:
var isStrongAchilles = Fn.new { |n|
if (!isAchilles.call(n)) return false
var tot = totientInt.calltotient(n)
return isAchilles.call(tot)
}
Line 3,361 ⟶ 3,346:
n = n + 1
}
Fmt.tprint("$4d", achilles, 10)
for (chunk in Lst.chunks(achilles, 10)) Fmt.print("$4d", chunk)
 
System.print("\nFirst 30 strong Achilles numbers:")
Line 3,374 ⟶ 3,359:
n = n + 1
}
Fmt.tprint("$5d", strongAchilles, 10)
for (chunk in Lst.chunks(strongAchilles, 10)) Fmt.print("$5d", chunk)
 
System.print("\nNumber of Achilles numbers with:")
Line 3,415 ⟶ 3,400:
 
Ridiculously fast compared to the previous method: 12 digits can now be reached in 1.03 seconds, 13 digits in 3.7 seconds, 14 digits in 12.2 seconds and 15 digits in 69 seconds.
<syntaxhighlight lang="ecmascriptwren">import "./set" for Set
import "./seq" for Lst
import "./math" for Int
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 pps = Set.new()
Line 3,469 ⟶ 3,440:
 
System.print("First 50 Achilles numbers:")
Fmt.tprint("$4d", achilles.take(50), 10)
for (chunk in Lst.chunks(achilles[0..49], 10)) Fmt.print("$4d", chunk)
 
System.print("\nFirst 30 strong Achilles numbers:")
Line 3,476 ⟶ 3,447:
var n = 0
while (count < 30) {
var tot = totientInt.calltotient(achilles[n])
if (achillesSet.contains(tot)) {
strongAchilles.add(achilles[n])
Line 3,483 ⟶ 3,454:
n = n + 1
}
Fmt.tprint("$5d", strongAchilles, 10)
for (chunk in Lst.chunks(strongAchilles, 10)) Fmt.print("$5d", chunk)
 
System.print("\nNumber of Achilles numbers with:")
9,485

edits