Jacobsthal numbers: Difference between revisions

Created Nim solution.
(Added Gambas)
(Created Nim solution.)
Line 2,042:
313 5562466239377370006237035693149875298444543026970449921737087520370363869220418099018130434731
347 95562442332919646317117537304253622533190207882011713489066201641121786503686867002917439712921903606443</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/strutils
 
func isPrime(n: Natural): bool =
## Return true if "n" is prime.
if n < 2: return false
if n mod 2 == 0: return n == 2
if n mod 3 == 0: return n == 3
var step = 2
var d = 5
while d * d <= n:
if n mod d == 0: return false
inc d, step
step = 6 - step
result = true
 
iterator jacobsthalSequence(first, second: int): int =
## Yield the successive Jacobsthal numbers or
## Jacobsthal-Lucas numbers.
var prev = first
var curr = second
yield prev
yield curr
while true:
swap prev, curr
curr += curr + prev
yield curr
 
iterator jacobsthalOblong(): int =
## Yield the successive Jacobsthal oblong numbers.
var prev = -1
for n in jacobsthalSequence(0, 1):
if prev >= 0:
yield prev * n
prev = n
 
iterator jacobsthalPrimes(): int =
## Yield the successive Jacobsthal prime numbers.
for n in jacobsthalSequence(0, 1):
if n.isPrime:
yield n
 
 
echo "First 30 Jacobsthal numbers:"
var count = 0
for n in jacobsthalSequence(0, 1):
inc count
stdout.write align($n, 11)
if count mod 6 == 0: echo()
if count == 30: break
 
echo "\nFirst 30 Jacobsthal-Lucas numbers:"
count = 0
for n in jacobsthalSequence(2, 1):
inc count
stdout.write align($n, 11)
if count mod 6 == 0: echo()
if count == 30: break
 
echo "\nFirst 20 Jacobsthal oblong numbers:"
count = 0
for n in jacobsthalOblong():
inc count
stdout.write align($n, 13)
if count mod 5 == 0: echo()
if count == 20: break
 
echo "\nFirst 10 Jacobsthal prime numbers:"
count = 0
for n in jacobsthalPrimes():
inc count
stdout.write align($n, 11)
if count mod 5 == 0: echo()
if count == 10: break
</syntaxhighlight>
 
{{out}}
<pre>First 30 Jacobsthal numbers:
0 1 1 3 5 11
21 43 85 171 341 683
1365 2731 5461 10923 21845 43691
87381 174763 349525 699051 1398101 2796203
5592405 11184811 22369621 44739243 89478485 178956971
 
First 30 Jacobsthal-Lucas numbers:
2 1 5 7 17 31
65 127 257 511 1025 2047
4097 8191 16385 32767 65537 131071
262145 524287 1048577 2097151 4194305 8388607
16777217 33554431 67108865 134217727 268435457 536870911
 
First 20 Jacobsthal oblong numbers:
0 1 3 15 55
231 903 3655 14535 58311
232903 932295 3727815 14913991 59650503
238612935 954429895 3817763271 15270965703 61084037575
 
First 10 Jacobsthal prime numbers:
3 5 11 43 683
2731 43691 174763 2796203 715827883
</pre>
 
=={{header|OCaml}}==
256

edits