Achilles numbers: Difference between revisions

m
→‎Version 2 (Much faster): Timing correction and other minor changes.
m (→‎Version 2 (Much faster): Timing correction and other minor changes.)
Line 492:
Here we make use of the fact that powerful numbers are always of the form a²b³, where a and b > 0, to generate such numbers up to a given limit. We also use an improved method for checking whether a number is a perfect power.
 
Ridiculously fast compared to the previous method: 8 digits now reached in 0.57 seconds, 9 digits in 4.82 seconds and 10 digits in 34 seconds. However, 11 digits takes 300 seconds and I gave up after that.
<lang ecmascript>import "./set" for Set, Bag
import "./math" for Int
Line 522:
}
 
var getAchilles = Fn.new { |digitsminDigits, lowermaxDigits|
var upperlower = 10.pow(digitsminDigits)
var upper = 10.pow(maxDigits)
var achilles = Set.new() // avoids duplicates
var count = 0
Line 539 ⟶ 540:
}
 
var achillesSet = getAchilles.call(51, 15) // enough for first 2 parts
var achilles = achillesSet.toList
achilles.sort()
Line 563 ⟶ 564:
System.print("\nNumber of Achilles numbers with:")
for (d in 2..maxDigits) {
var ac = getAchilles.call(d-1, 10.pow(d-1)).count
Fmt.print("$2d digits: $d", d, ac)
}</lang>
9,485

edits