Self numbers: Difference between revisions

Added Wren
(→‎{{header|Go}}: Added a second much faster version.)
(Added Wren)
Line 346:
 
real 0m18,764s
 
=={{header|Wren}}==
{{trans|Go}}
Just the sieve based version as the low memory version would take too long to run in Wren.
 
Note that you need a lot of memory to run this as Bools in Wren require 8 bytes of storage compared to 1 byte in Go.
 
Unsurprisingly, very slow compared to the Go version as Wren is interpreted and uses floating point arithmetic for all numerical work.
<lang ecmascript>var sieve = Fn.new {
var sv = List.filled(2*1e9+9*9+1, false)
var n = 0
for (a in 0..1) {
for (b in 0..9) {
for (c in 0..9) {
for (d in 0..9) {
for (e in 0..9) {
for (f in 0..9) {
for (g in 0..9) {
for (h in 0..9) {
for (i in 0..9) {
for (j in 0..9) {
sv[a + b + c + d + e + f + g + h + i + j + n] = true
n = n + 1
}
}
}
}
}
}
}
}
}
}
return sv
}
 
var st = System.clock
var sv = sieve.call()
var count = 0
System.print("The first 50 self numbers are:")
for (i in 0...sv.count) {
if (!sv[i]) {
count = count + 1
if (count <= 50) System.write("%(i) ")
if (count == 1e8) {
System.print("\n\nThe 100 millionth self number is %(i)")
break
}
}
}
System.print("Took %(System.clock-st) seconds.")</lang>
 
{{out}}
<pre>
The first 50 self numbers are:
1 3 5 7 9 20 31 42 53 64 75 86 97 108 110 121 132 143 154 165 176 187 198 209 211 222 233 244 255 266 277 288 299 310 312 323 334 345 356 367 378 389 400 411 413 424 435 446 457 468
 
The 100 millionth self number is 1022727208
Took 314.869302 seconds.
</pre>
9,488

edits