Achilles numbers: Difference between revisions

Content deleted Content added
PureFox (talk | contribs)
PureFox (talk | contribs)
m →‎Version 2 (Much faster): Timing correction and other minor changes.
Line 492: 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.
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.8 seconds and 10 digits in 34 seconds. However, 11 digits takes 300 seconds and I gave up after that.
Ridiculously fast compared to the previous method: 8 digits now reached in 0.57 seconds, 9 digits in 4.2 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
<lang ecmascript>import "./set" for Set, Bag
import "./math" for Int
import "./math" for Int
Line 522: Line 522:
}
}


var getAchilles = Fn.new { |digits, lower|
var getAchilles = Fn.new { |minDigits, maxDigits|
var upper = 10.pow(digits)
var lower = 10.pow(minDigits)
var upper = 10.pow(maxDigits)
var achilles = Set.new() // avoids duplicates
var achilles = Set.new() // avoids duplicates
var count = 0
var count = 0
Line 539: Line 540:
}
}


var achillesSet = getAchilles.call(5, 1) // enough for first 2 parts
var achillesSet = getAchilles.call(1, 5) // enough for first 2 parts
var achilles = achillesSet.toList
var achilles = achillesSet.toList
achilles.sort()
achilles.sort()
Line 563: Line 564:
System.print("\nNumber of Achilles numbers with:")
System.print("\nNumber of Achilles numbers with:")
for (d in 2..maxDigits) {
for (d in 2..maxDigits) {
var ac = getAchilles.call(d, 10.pow(d-1)).count
var ac = getAchilles.call(d-1, d).count
Fmt.print("$2d digits: $d", d, ac)
Fmt.print("$2d digits: $d", d, ac)
}</lang>
}</lang>