Wolstenholme numbers: Difference between revisions
Content added Content deleted
(Added Ada solution) |
(Created Nim solution.) |
||
Line 545: | Line 545: | ||
15: 40306783143871607599250..98658901192511859288941 (1539 digits) |
15: 40306783143871607599250..98658901192511859288941 (1539 digits) |
||
</pre> |
</pre> |
||
=={{header|Nim}}== |
|||
===Task=== |
|||
Using only the standard library modules. |
|||
<syntaxhighlight lang="Nim">import std/rationals |
|||
func isPrime(n: Natural): bool = |
|||
if n < 2: return false |
|||
if (n and 1) == 0: return n == 2 |
|||
if n mod 3 == 0: return n == 3 |
|||
var k = 5 |
|||
while k * k <= n: |
|||
if n mod k == 0 or n mod (k + 2) == 0: return false |
|||
inc k, 6 |
|||
result = true |
|||
iterator wolstenholme(): (int, int) = |
|||
var count = 0 |
|||
var k = 1 |
|||
var s = 1.toRational |
|||
while true: |
|||
inc count |
|||
yield (count, s.num) |
|||
inc k |
|||
s += 1 // (k * k) |
|||
echo "First 20 Wolstenholme numbers:" |
|||
var wprimes: seq[int] |
|||
for (count, n) in wolstenholme(): |
|||
echo n |
|||
if n.isPrime: |
|||
wprimes.add n |
|||
if count == 20: break |
|||
echo "\nFirst 4 Wolstenholme primes:" |
|||
for i in 0..3: |
|||
echo wprimes[i] |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>First 20 Wolstenholme numbers: |
|||
1 |
|||
5 |
|||
49 |
|||
205 |
|||
5269 |
|||
5369 |
|||
266681 |
|||
1077749 |
|||
9778141 |
|||
1968329 |
|||
239437889 |
|||
240505109 |
|||
40799043101 |
|||
40931552621 |
|||
205234915681 |
|||
822968714749 |
|||
238357395880861 |
|||
238820721143261 |
|||
86364397717734821 |
|||
17299975731542641 |
|||
First 4 Wolstenholme primes: |
|||
5 |
|||
266681 |
|||
40799043101 |
|||
86364397717734821</pre> |
|||
===Stretch task=== |
|||
{{libheader|bignum}} |
|||
<syntaxhighlight lang="Nim">import std/strformat |
|||
import bignum |
|||
iterator wolstenholme(): (int, Int) = |
|||
var count = 0 |
|||
var k = 1 |
|||
var s = newRat(1) |
|||
while true: |
|||
inc count |
|||
yield (count, s.num) |
|||
inc k |
|||
s += newRat(1, k * k) |
|||
var wprimes: seq[Int] |
|||
for (count, n) in wolstenholme(): |
|||
if wprimes.len < 15 and probablyPrime(n, 25) != 0: |
|||
wprimes.add n |
|||
if count in [500, 1000, 2500, 5000, 10000]: |
|||
echo &"The {count}th Wolstenholme number has {len($n)} digits." |
|||
if count == 10000: break |
|||
echo "\nDigit count of the first 15 Wolstenholme primes:" |
|||
for i in 0..14: |
|||
echo &"{i + 1: >2}: {len($wprimes[i])}" |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The 500th Wolstenholme number has 434 digits. |
|||
The 1000th Wolstenholme number has 866 digits. |
|||
The 2500th Wolstenholme number has 2164 digits. |
|||
The 5000th Wolstenholme number has 4340 digits. |
|||
The 10000th Wolstenholme number has 8693 digits. |
|||
Digit count of the first 15 Wolstenholme primes: |
|||
1: 1 |
|||
2: 6 |
|||
3: 11 |
|||
4: 17 |
|||
5: 104 |
|||
6: 156 |
|||
7: 218 |
|||
8: 318 |
|||
9: 520 |
|||
10: 649 |
|||
11: 935 |
|||
12: 984 |
|||
13: 1202 |
|||
14: 1518 |
|||
15: 1539</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |