Factorial primes: Difference between revisions
Content added Content deleted
No edit summary |
(Nim solution.) |
||
Line 882: | Line 882: | ||
9: 12! - 1 = 479001599 |
9: 12! - 1 = 479001599 |
||
10: 14! - 1 = 87178291199 |
10: 14! - 1 = 87178291199 |
||
</pre> |
|||
=={{header|Nim}}== |
|||
{{libheader|integers}} |
|||
Nim standard integer types are limited to 64 bits. So we use an external library which provides arbitrary sized integers. |
|||
<syntaxhighlight lang="nim">import std/[math, strformat] |
|||
# Task. |
|||
func isPrime(n: int): bool = |
|||
if n < 2: return false |
|||
if n == 2 or n == 3: return true |
|||
if n mod 2 == 0: return false |
|||
if n mod 3 == 0: return false |
|||
var d = 5 |
|||
var step = 2 |
|||
while d * d <= n: |
|||
if n mod d == 0: |
|||
return false |
|||
inc d, step |
|||
step = 6 - step |
|||
return true |
|||
echo "First 10 factorial primes:\n" |
|||
var count = 0 |
|||
var n = 1 |
|||
while count < 10: |
|||
let f = fac(n) |
|||
if isPrime(f - 1): |
|||
inc count |
|||
echo &"{count:>2}: {n:>3}! - 1 = {f - 1}" |
|||
if count < 10 and isPrime(f + 1): |
|||
inc count |
|||
echo &"{count:>2}: {n:>3}! + 1 = {f + 1}" |
|||
inc n |
|||
# Stretch. |
|||
import integers |
|||
func str(n: Integer): string = |
|||
## Return the string representation of an Integer. |
|||
result = $n |
|||
if result.len > 40: |
|||
result = &"{result[0..19]}...{result[^20..^1]} ({result.len} digits)" |
|||
echo "\n\nNext 20 factorial primes:\n" |
|||
while count < 30: |
|||
let f: Integer = factorial(n) |
|||
if isPrime(f - 1): |
|||
inc count |
|||
echo &"{count:>2}: {n:>3}! - 1 = {str(f - 1)}" |
|||
if isPrime(f + 1): |
|||
inc count |
|||
echo &"{count:>2}: {n:>3}! - 1 = {str(f + 1)}" |
|||
inc n |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>First 10 factorial primes: |
|||
1: 1! + 1 = 2 |
|||
2: 2! + 1 = 3 |
|||
3: 3! - 1 = 5 |
|||
4: 3! + 1 = 7 |
|||
5: 4! - 1 = 23 |
|||
6: 6! - 1 = 719 |
|||
7: 7! - 1 = 5039 |
|||
8: 11! + 1 = 39916801 |
|||
9: 12! - 1 = 479001599 |
|||
10: 14! - 1 = 87178291199 |
|||
Next 20 factorial primes: |
|||
11: 27! - 1 = 10888869450418352160768000001 |
|||
12: 30! - 1 = 265252859812191058636308479999999 |
|||
13: 32! - 1 = 263130836933693530167218012159999999 |
|||
14: 33! - 1 = 8683317618811886495518194401279999999 |
|||
15: 37! - 1 = 13763753091226345046...79581580902400000001 (44 digits) |
|||
16: 38! - 1 = 52302261746660111176...24100074291199999999 (45 digits) |
|||
17: 41! - 1 = 33452526613163807108...40751665152000000001 (50 digits) |
|||
18: 73! - 1 = 44701154615126843408...03680000000000000001 (106 digits) |
|||
19: 77! - 1 = 14518309202828586963...48000000000000000001 (114 digits) |
|||
20: 94! - 1 = 10873661566567430802...99999999999999999999 (147 digits) |
|||
21: 116! - 1 = 33931086844518982011...00000000000000000001 (191 digits) |
|||
22: 154! - 1 = 30897696138473508879...00000000000000000001 (272 digits) |
|||
23: 166! - 1 = 90036917057784373664...99999999999999999999 (298 digits) |
|||
24: 320! - 1 = 21161033472192524829...00000000000000000001 (665 digits) |
|||
25: 324! - 1 = 22889974601791023211...99999999999999999999 (675 digits) |
|||
26: 340! - 1 = 51008644721037110809...00000000000000000001 (715 digits) |
|||
27: 379! - 1 = 24840307460964707050...99999999999999999999 (815 digits) |
|||
28: 399! - 1 = 16008630711655973815...00000000000000000001 (867 digits) |
|||
29: 427! - 1 = 29063471769607348411...00000000000000000001 (940 digits) |
|||
30: 469! - 1 = 67718096668149510900...99999999999999999999 (1051 digits) |
|||
</pre> |
</pre> |
||