Jump to content

Pan base non-primes: Difference between revisions

Added Wren
(Added Wren)
Line 96:
Percent odd up to and including 2500: 16.894019
Percent even up to and including 2500: 83.105981</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "./math" for Int
import "./fmt" for Fmt
 
var strToDec = Fn.new { |s, b|
var res = 0
for (c in s) {
var d = Num.fromString(c)
res = res * b + d
}
return res
}
 
var limit = 2500
var pbnp = []
for (n in 3..limit) {
if (n % 10 == 0 && n > 10) {
pbnp.add(n)
} else if (n > 9 && Int.gcd(Int.digits(n)) > 1) {
pbnp.add(n)
} else {
var comp = true
for (b in 2...n) {
var d = strToDec.call(n.toString, b)
if (Int.isPrime(d)) {
comp = false
break
}
}
if (comp) pbnp.add(n)
}
}
 
System.print("First 50 pan-base composites:")
Fmt.tprint("$3d", pbnp[0..49], 10)
 
System.print("\nFirst 20 odd pan-base composites:")
var odd = pbnp.where { |n| n % 2 == 1 }.toList
Fmt.tprint("$3d", odd[0..19], 10)
 
var tc
System.print("\nCount of pan-base composites up to and including %(limit): %(tc = pbnp.count)")
var c
Fmt.print("Number odd = $3d or $9.6f\%", c = odd.count, c/tc * 100)
Fmt.print("Number even = $3d or $9.6f\%", c = tc - c, c/tc * 100) </lang>
 
{{out}}
<pre>
First 50 pan-base composites:
4 6 8 9 20 22 24 26 28 30
33 36 39 40 42 44 46 48 50 55
60 62 63 64 66 68 69 70 77 80
82 84 86 88 90 93 96 99 100 110
112 114 116 118 120 121 130 132 134 136
 
First 20 odd pan-base composites:
9 33 39 55 63 69 77 93 99 121
143 165 169 187 231 253 273 275 297 299
 
Count of pan-base composites up to and including 2500: 953
Number odd = 161 or 16.894019%
Number even = 792 or 83.105981%
</pre>
9,490

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.