Pan base non-primes: Difference between revisions

Created Nim solution.
m (→‎{{header|Ruby}}: expanded comment)
(Created Nim solution.)
Line 582:
Odd up to and including 2500: 161//953, or 16.89%.
Even up to and including 2500: 792//953, or 83.1%.
</pre>
 
=={{header|Nim}}==
{{trans|C}}
<syntaxhighlight lang="Nim">import std/strformat
 
func isPrime(n: int64): bool =
const Wheel = [4, 2, 4, 2, 4, 6, 2, 6]
if n < 2: return false
if (n and 1) == 0: return n == 2
if n mod 3 == 0: return n == 3
if n mod 5 == 0: return n == 5
var p = 7
while true:
for w in Wheel:
if p * p > n: return true
if n mod p == 0: return false
inc p, w
 
func digits(n: int64): seq[byte] =
## Compute the digits of n in base 10, least significant digit first.
var n = n
while n != 0:
result.add byte(n mod 10)
n = n div 10
 
func fromDigits(a: seq[byte]; base: Positive): int64 =
## Convert digits in the given base to a number (least significant digit first).
for i in countdown(a.high, 0):
result = result * base + a[i].int
 
func isPanBaseNonPrime(n: int64): bool =
if n < 10: return not n.isPrime()
if n > 10 and n mod 10 == 0: return true
let d = n.digits
let maxDigit = max(d).int
for base in (maxDigit + 1)..n:
if d.fromDigits(base).isPrime():
return false
result = true
 
 
echo "First 50 prime pan-base composites:"
var count = 0;
var n = 2
while count < 50:
if n.isPanBaseNonPrime():
inc count
stdout.write &"{n:3}", if count mod 10 == 0: '\n' else: ' '
inc n
 
echo "\nFirst 20 odd prime pan-base composites:"
count = 0
n = 3
while count < 20:
if n.isPanBaseNonPrime():
inc count
stdout.write &"{n:3}", if count mod 10 == 0: '\n' else: ' '
inc n, 2
 
const Limit = 10_000
var odd = 0
count = 0
for n in 2..Limit:
if n.isPanBaseNonPrime():
inc count
if (n and 1) == 1:
inc odd
echo &"\nCount of pan-base composites up to and including {Limit}: {count}"
let percent = 100 * odd / count
echo &"Percent odd up to and including {Limit}: {percent:.6f}"
echo &"Percent even up to and including {Limit}: {100 - percent:.6f}"
</syntaxhighlight>
 
{{out}}
<pre>First 50 prime 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 prime 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 10000: 3849
Percent odd up to and including 10000: 18.030657
Percent even up to and including 10000: 81.969343
</pre>
 
256

edits