Deceptive numbers: Difference between revisions

Content deleted Content added
Petelomax (talk | contribs)
m →‎{{header|Phix}}: removed two unnecessary "!=0"s
PureFox (talk | contribs)
→‎{{header|Wren}}: Moved to initializing repunits from integers rather than strings - 10 x speedup.
Line 130: Line 130:
{{libheader|Wren-gmp}}
{{libheader|Wren-gmp}}
{{libheader|Wren-math}}
{{libheader|Wren-math}}
An embedded program so we can use GMP. Takes 0.207 seconds to find the first 25 deceptive numbers.
An embedded program so we can use GMP. Takes 0.021 seconds to find the first 25 deceptive numbers.
<lang ecmascript>/* deceptive_numbers.wren */
<lang ecmascript>/* deceptive_numbers.wren */


Line 137: Line 137:


var count = 0
var count = 0
var limit = 25
var limit = 80
var n = 25
var n = 17
var repunit = Mpz.from(1111111111111111)
var s = "1" * 24
var deceptive = []
var deceptive = []
while (count < limit) {
while (count < limit) {
if (!Int.isPrime(n) && n % 3 != 0) {
if (!Int.isPrime(n) && n % 3 != 0) {
var repunit = Mpz.fromStr(s)
if (repunit.isDivisibleUi(n)) {
if (repunit.isDivisibleUi(n)) {
deceptive.add(n)
deceptive.add(n)
Line 150: Line 149:
}
}
n = n + 2
n = n + 2
s = s + "11"
repunit.mul(100).add(11)
}
}
System.print("The first %(limit) deceptive numbers are:")
System.print("The first %(limit) deceptive numbers are:")