Jacobsthal numbers: Difference between revisions

Add C# implementation
(Add C# implementation)
 
(46 intermediate revisions by 23 users not shown)
Line 49:
 
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
F isPrime(n)
L(i) 2 .. Int(n ^ 0.5)
I n % i == 0
R 0B
R 1B
 
F odd(n)
R n [&] 1 != 0
 
F jacobsthal(n)
R floori((pow(2.0, n) + odd(n)) / 3)
 
F jacobsthal_lucas(n)
R Int(pow(2, n) + pow(-1, n))
 
F jacobsthal_oblong(n)
R Int64(jacobsthal(n)) * jacobsthal(n + 1)
 
print(‘First 30 Jacobsthal numbers:’)
L(j) 0..29
print(jacobsthal(j), end' ‘ ’)
 
print("\n\nFirst 30 Jacobsthal-Lucas numbers: ")
L(j) 0..29
print(jacobsthal_lucas(j), end' "\t")
 
print("\n\nFirst 20 Jacobsthal oblong numbers: ")
L(j) 0..19
print(jacobsthal_oblong(j), end' ‘ ’)
 
print("\n\nFirst 10 Jacobsthal primes: ")
L(j) 3..32
I isPrime(jacobsthal(j))
print(jacobsthal(j))
</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 primes:
3
5
11
43
683
2731
43691
174763
2796203
715827883
</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find some Jacobsthal and related Numbers #
INT max jacobsthal = 29; # highest Jacobsthal number we will find #
INT max oblong = 20; # highest Jacobsthal oblong number we will find #
Line 103 ⟶ 167:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 150 ⟶ 214:
 
=={{header|AppleScript}}==
===Procedural===
<lang applescript>on jacobsthalNumbers(variant, n)
<syntaxhighlight lang="applescript">on jacobsthalNumbers(variant, n)
-- variant: text containing "Lucas", "oblong", or "prime" — or none of these.
-- n: length of output sequence required.
Line 283 ⟶ 348:
 
task()
</syntaxhighlight>
</lang>
 
{{output}}
<langsyntaxhighlight lang="applescript">"First 30 Jacobsthal Numbers:
0 1 1 3 5 11 21 43 85 171
341 683 1365 2731 5461 10923 21845 43691 87381 174763
Line 302 ⟶ 367:
First 11 Jacobsthal Primes:
3 5 11 43 683 2731
43691 174763 2796203 715827883 2932031007403"</langsyntaxhighlight>
 
===Functional===
<syntaxhighlight lang="applescript">-------------------- JACOBSTHAL NUMBERS ------------------
 
-- e.g. take(10, jacobsthal())
 
-- jacobsthal :: [Int]
on jacobsthal()
-- The terms of OEIS:A001045 as a non-finite sequence.
jacobsthalish(0, 1)
end jacobsthal
 
 
-- jacobsthal :: (Int, Int) -> [Int]
on jacobsthalish(x, y)
-- An infinite sequence of the terms of the
-- Jacobsthal-type series which begins with x and y.
script go
on |λ|(ab)
set {a, b} to ab
{a, {b, (2 * a) + b}}
end |λ|
end script
unfoldr(go, {x, y})
end jacobsthalish
 
 
-------------------------- TESTS -------------------------
on run
unlines(map(fShow, {¬
{"terms of the Jacobsthal sequence", ¬
30, jacobsthal()}, ¬
{"Jacobsthal-Lucas numbers", ¬
30, jacobsthalish(2, 1)}, ¬
{"Jacobsthal oblong numbers", ¬
20, zipWith(my mul, jacobsthal(), drop(1, jacobsthal()))}, ¬
{"primes in the Jacobsthal sequence", ¬
10, filter(isPrime, jacobsthal())}}))
end run
 
 
------------------------ FORMATTING ----------------------
on fShow(test)
set {k, n, xs} to test
str(n) & " first " & k & ":" & linefeed & ¬
table(5, map(my str, take(n, xs))) & linefeed
end fShow
 
 
-- justifyRight :: Int -> Char -> String -> String
on justifyRight(n, cFiller)
script go
on |λ|(s)
if n > length of s then
text -n thru -1 of ((replicate(n, cFiller) as text) & s)
else
s
end if
end |λ|
end script
end justifyRight
 
 
-- Egyptian multiplication - progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for binary
-- assembly of a target length
-- replicate :: Int -> String -> String
on replicate(n, s)
-- Egyptian multiplication - progressively doubling a list,
-- appending stages of doubling to an accumulator where needed
-- for binary assembly of a target length
script p
on |λ|({n})
n ≤ 1
end |λ|
end script
script f
on |λ|({n, dbl, out})
if (n mod 2) > 0 then
set d to out & dbl
else
set d to out
end if
{n div 2, dbl & dbl, d}
end |λ|
end script
set xs to |until|(p, f, {n, s, ""})
item 2 of xs & item 3 of xs
end replicate
 
 
-- table :: Int -> [String] -> String
on table(n, xs)
-- A list of strings formatted as
-- right-justified rows of n columns.
set w to length of last item of xs
unlines(map(my unwords, ¬
chunksOf(n, map(justifyRight(w, space), xs))))
end table
 
 
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines
 
 
-- until :: (a -> Bool) -> (a -> a) -> a -> a
on |until|(p, f, x)
set v to x
set mp to mReturn(p)
set mf to mReturn(f)
repeat until mp's |λ|(v)
set v to mf's |λ|(v)
end repeat
v
end |until|
 
 
-- unwords :: [String] -> String
on unwords(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, space}
set s to xs as text
set my text item delimiters to dlm
return s
end unwords
 
 
------------------------- GENERIC ------------------------
 
-- Just :: a -> Maybe a
on Just(x)
-- Constructor for an inhabited Maybe (option type) value.
-- Wrapper containing the result of a computation.
{type:"Maybe", Nothing:false, Just:x}
end Just
 
 
-- Nothing :: Maybe a
on Nothing()
-- Constructor for an empty Maybe (option type) value.
-- Empty wrapper returned where a computation is not possible.
{type:"Maybe", Nothing:true}
end Nothing
 
 
-- abs :: Num -> Num
on abs(x)
-- Absolute value.
if 0 > x then
-x
else
x
end if
end abs
 
 
-- any :: (a -> Bool) -> [a] -> Bool
on any(p, xs)
-- Applied to a predicate and a list,
-- |any| returns true if at least one element of the
-- list satisfies the predicate.
tell mReturn(p)
set lng to length of xs
repeat with i from 1 to lng
if |λ|(item i of xs) then return true
end repeat
false
end tell
end any
 
 
-- chunksOf :: Int -> [a] -> [[a]]
on chunksOf(k, xs)
script
on go(ys)
set ab to splitAt(k, ys)
set a to item 1 of ab
if {} ≠ a then
{a} & go(item 2 of ab)
else
a
end if
end go
end script
result's go(xs)
end chunksOf
 
 
-- drop :: Int -> [a] -> [a]
-- drop :: Int -> String -> String
on drop(n, xs)
take(n, xs) -- consumed
xs
end drop
 
 
-- enumFromThenTo :: Int -> Int -> Int -> [Int]
on enumFromThenTo(x1, x2, y)
set xs to {}
set gap to x2 - x1
set d to max(1, abs(gap)) * (signum(gap))
repeat with i from x1 to y by d
set end of xs to i
end repeat
return xs
end enumFromThenTo
 
 
-- filter :: (a -> Bool) -> Gen [a] -> Gen [a]
on filter(p, gen)
-- Non-finite stream of values which are
-- drawn from gen, and satisfy p
script
property mp : mReturn(p)'s |λ|
on |λ|()
set v to gen's |λ|()
repeat until mp(v)
set v to gen's |λ|()
end repeat
return v
end |λ|
end script
end filter
 
 
-- isPrime :: Int -> Bool
on isPrime(n)
-- True if n is prime
if {2, 3} contains n then return true
if 2 > n or 0 = (n mod 2) then return false
if 9 > n then return true
if 0 = (n mod 3) then return false
script p
on |λ|(x)
0 = n mod x or 0 = n mod (2 + x)
end |λ|
end script
not any(p, enumFromThenTo(5, 11, 1 + (n ^ 0.5)))
end isPrime
 
 
-- length :: [a] -> Int
on |length|(xs)
set c to class of xs
if list is c or string is c then
length of xs
else
(2 ^ 29 - 1) -- (maxInt - simple proxy for non-finite)
end if
end |length|
 
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
 
-- max :: Ord a => a -> a -> a
on max(x, y)
if x > y then
x
else
y
end if
end max
 
 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
 
-- mul (*) :: Num a => a -> a -> a
on mul(a, b)
a * b
end mul
 
 
-- signum :: Num -> Num
on signum(x)
if x < 0 then
-1
else if x = 0 then
0
else
1
end if
end signum
 
 
-- splitAt :: Int -> [a] -> ([a], [a])
on splitAt(n, xs)
if n > 0 and n < length of xs then
if class of xs is text then
{items 1 thru n of xs as text, ¬
items (n + 1) thru -1 of xs as text}
else
{items 1 thru n of xs, items (n + 1) thru -1 of xs}
end if
else
if n < 1 then
{{}, xs}
else
{xs, {}}
end if
end if
end splitAt
 
 
-- str :: a -> String
on str(x)
x as string
end str
 
 
-- take :: Int -> [a] -> [a]
-- take :: Int -> String -> String
on take(n, xs)
set ys to {}
repeat with i from 1 to n
set v to |λ|() of xs
if missing value is v then
return ys
else
set end of ys to v
end if
end repeat
return ys
end take
 
 
-- uncons :: [a] -> Maybe (a, [a])
on uncons(xs)
set lng to |length|(xs)
if 0 = lng then
Nothing()
else
if (2 ^ 29 - 1) as integer > lng then
if class of xs is string then
set cs to text items of xs
Just({item 1 of cs, rest of cs})
else
Just({item 1 of xs, rest of xs})
end if
else
set nxt to take(1, xs)
if {} is nxt then
Nothing()
else
Just({item 1 of nxt, xs})
end if
end if
end if
end uncons
 
 
-- unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
on unfoldr(f, v)
-- A lazy (generator) list unfolded from a seed value
-- by repeated application of f to a value until no
-- residue remains. Dual to fold/reduce.
-- f returns either nothing (missing value)
-- or just (value, residue).
script
property valueResidue : {v, v}
property g : mReturn(f)
on |λ|()
set valueResidue to g's |λ|(item 2 of (valueResidue))
if missing value ≠ valueResidue then
item 1 of (valueResidue)
else
missing value
end if
end |λ|
end script
end unfoldr
 
 
-- zipWith :: (a -> b -> c) -> Gen [a] -> Gen [b] -> Gen [c]
on zipWith(f, ga, gb)
script
property ma : missing value
property mb : missing value
property mf : mReturn(f)
on |λ|()
if missing value is ma then
set ma to uncons(ga)
set mb to uncons(gb)
end if
if Nothing of ma or Nothing of mb then
missing value
else
set ta to Just of ma
set tb to Just of mb
set ma to uncons(item 2 of ta)
set mb to uncons(item 2 of tb)
|λ|(item 1 of ta, item 1 of tb) of mf
end if
end |λ|
end script
end zipWith</syntaxhighlight>
{{Out}}
<pre>30 first terms of the Jacobsthal sequence:
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
 
30 first 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
 
20 first Jacobsthal oblong numbers:
0 1 3 15 55
231 903 3655 14535 58311
232903 932295 3727815 14913991 59650503
238612935 9.54429895E+8 3.817763271E+9 1.5270965703E+10 6.1084037575E+10
 
10 first primes in the Jacobsthal sequence:
3 5 11 43 683
2731 43691 174763 2796203 7.15827883E+8</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">J: function [n]-> ((2^n) - (neg 1)^n)/3
JL: function [n]-> (2^n) + (neg 1)^n
JO: function [n]-> (J n) * (J n+1)
 
printFirst: function [label, what, predicate, count][
print ["First" count label++":"]
result: new []
i: 0
while [count > size result][
num: do ~"|what| i"
if do predicate -> 'result ++ num
i: i + 1
]
 
(predicate=[true])? [
loop split.every: 5 result 'row [
print map to [:string] row 'item -> pad item 12
]
][
loop result 'row -> print row
]
print ""
]
 
printFirst "Jacobsthal numbers" 'J [true] 30
printFirst "Jacobsthal-Lucas numbers" 'JL [true] 30
printFirst "Jacobsthal oblong numbers" 'JO [true] 20
printFirst "Jacobsthal primes" 'J [prime? num] 20</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 20 Jacobsthal primes:
3
5
11
43
683
2731
43691
174763
2796203
715827883
2932031007403
768614336404564651
201487636602438195784363
845100400152152934331135470251
56713727820156410577229101238628035243
62357403192785191176690552862561408838653121833643
1046183622564446793972631570534611069350392574077339085483
267823007376498379256993682056860433753700498963798805883563
5562466239377370006237035693149875298444543026970449921737087520370363869220418099018130434731
95562442332919646317117537304253622533190207882011713489066201641121786503686867002917439712921903606443</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Jacobsthal(n){
return SubStr(" " Format("{:.0f}", (2**n - (-1)**n ) / 3), -8)
}
 
Jacobsthal_Lucas(n){
return SubStr(" " Format("{:.0f}", 2**n + (-1)**n), -8)
}
 
prime_numbers(n) {
if (n <= 3)
return [n]
ans := [], done := false
while !done {
if !Mod(n,2)
ans.push(2), n /= 2
else if !Mod(n,3)
ans.push(3), n /= 3
else if (n = 1)
return ans
else {
sr := sqrt(n), done := true, i := 6
while (i <= sr+6) {
if !Mod(n, i-1) { ; is n divisible by i-1?
ans.push(i-1), n /= i-1, done := false
break
}
if !Mod(n, i+1) { ; is n divisible by i+1?
ans.push(i+1), n /= i+1, done := false
break
}
i += 6
}}}
ans.push(n)
return ans
}</syntaxhighlight>
Examples:<syntaxhighlight lang="autohotkey">result := "First 30 Jacobsthal numbers:`n"
loop 30
result .= Jacobsthal(A_Index-1) (mod(A_Index, 5) ? " ":"`n")
 
result .= "`nFirst 30 Jacobsthal-Lucas numbers:`n"
loop 30
result .= Jacobsthal_Lucas(A_Index-1) (mod(A_Index, 5) ? " ":"`n")
 
result .= "`nFirst 20 Jacobsthal oblong numbers:`n"
loop 20
result .= SubStr(" " Jacobsthal(A_Index-1) * Jacobsthal(A_Index), -8) (mod(A_Index, 5) ? " ":"`n")
 
result .= "`nFirst 10 Jacobsthal primes:`n"
c:=0
while c < 10
if (prime_numbers(x:=Jacobsthal(A_Index)).Count() = 1 && x > 1)
result .= x (mod(++c, 5) ? " ":"`n")
 
MsgBox, 262144, , % result
return</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 817763271 270965703 084037575
 
First 10 Jacobsthal primes:
3 5 11 43 683
2731 43691 174763 2796203 715827883</pre>
 
=={{header|C}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <gmp.h>
 
Line 366 ⟶ 1,055:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 413 ⟶ 1,102:
5562466239377370006237035693149875298444543026970449921737087520370363869220418099018130434731
95562442332919646317117537304253622533190207882011713489066201641121786503686867002917439712921903606443
</pre>
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Numerics;
using System.Threading;
 
public class JacobsthalNumbers
{
private static BigInteger currentJacobsthal = 0;
private static int latestIndex = 0;
private static readonly BigInteger Three = new BigInteger(3);
private const int Certainty = 20;
public static void Main(string[] args)
{
Console.WriteLine("The first 30 Jacobsthal Numbers:");
for (int i = 0; i < 6; i++)
{
for (int k = 0; k < 5; k++)
{
Console.Write($"{JacobsthalNumber(i * 5 + k), 15}");
}
 
Console.WriteLine();
}
 
Console.WriteLine();
Console.WriteLine("The first 30 Jacobsthal-Lucas Numbers:");
for (int i = 0; i < 6; i++)
{
for (int k = 0; k < 5; k++)
{
Console.Write($"{JacobsthalLucasNumber(i * 5 + k), 15}");
}
 
Console.WriteLine();
}
 
Console.WriteLine();
Console.WriteLine("The first 20 Jacobsthal oblong Numbers:");
for (int i = 0; i < 4; i++)
{
for (int k = 0; k < 5; k++)
{
Console.Write($"{JacobsthalOblongNumber(i * 5 + k), 15}");
}
 
Console.WriteLine();
}
 
Console.WriteLine();
Console.WriteLine("The first 10 Jacobsthal Primes:");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(JacobsthalPrimeNumber());
}
}
 
private static BigInteger JacobsthalNumber(int index)
{
BigInteger value = new BigInteger(ParityValue(index));
return ((BigInteger.Parse("1") << index) - value) / Three;
}
 
private static long JacobsthalLucasNumber(int index)
{
return (1L << index) + ParityValue(index);
}
 
private static long JacobsthalOblongNumber(int index)
{
BigInteger nextJacobsthal = JacobsthalNumber(index + 1);
long result = (long)(currentJacobsthal * nextJacobsthal);
currentJacobsthal = nextJacobsthal;
return result;
}
 
private static long JacobsthalPrimeNumber()
{
BigInteger candidate = JacobsthalNumber(latestIndex++);
while (!candidate.IsProbablyPrime(Certainty))
{
candidate = JacobsthalNumber(latestIndex++);
}
 
return (long)candidate;
}
 
private static int ParityValue(int index)
{
return (index & 1) == 0 ? +1 : -1;
}
}
 
 
public static class BigIntegerExtensions
{
private static Random random = new Random();
 
public static bool IsProbablyPrime(this BigInteger source, int certainty)
{
if (source == 2 || source == 3)
return true;
if (source < 2 || source % 2 == 0)
return false;
 
BigInteger d = source - 1;
int s = 0;
 
while (d % 2 == 0)
{
d /= 2;
s += 1;
}
 
for (int i = 0; i < certainty; i++)
{
BigInteger a = RandomBigInteger(2, source - 2);
BigInteger x = BigInteger.ModPow(a, d, source);
if (x == 1 || x == source - 1)
continue;
 
for (int r = 1; r < s; r++)
{
x = BigInteger.ModPow(x, 2, source);
if (x == 1)
return false;
if (x == source - 1)
break;
}
 
if (x != source - 1)
return false;
}
 
return true;
}
 
private static BigInteger RandomBigInteger(BigInteger minValue, BigInteger maxValue)
{
if (minValue > maxValue)
throw new ArgumentException("minValue must be less than or equal to maxValue");
 
BigInteger range = maxValue - minValue + 1;
int length = range.ToByteArray().Length;
byte[] buffer = new byte[length];
 
BigInteger result;
do
{
random.NextBytes(buffer);
buffer[buffer.Length - 1] &= 0x7F; // Ensure non-negative
result = new BigInteger(buffer);
} while (result < minValue || result >= maxValue);
 
return result;
}
}
</syntaxhighlight>
{{out}}
<pre>
The 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
 
The 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
 
The 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
 
The first 10 Jacobsthal Primes:
3
5
11
43
683
2731
43691
174763
2796203
715827883
 
</pre>
 
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <gmpxx.h>
 
#include <iomanip>
Line 464 ⟶ 1,350:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 511 ⟶ 1,397:
5562466239377370006237035693149875298444543026970449921737087520370363869220418099018130434731
95562442332919646317117537304253622533190207882011713489066201641121786503686867002917439712921903606443
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
procedure GetJacobsthalNum(Lucas: boolean; Max: integer; var IA: TInt64DynArray);
{Get Jacobsthal number sequence. If Lucas is true do Lucal variation}
var I: integer;
begin
SetLength(IA,Max);
{Lucas starts sequence with 2 instead of 0}
if Lucas then IA[0]:=2 else IA[0]:=0;
IA[1]:=1;
{Calculate Nn = Nn-1 + 2 Nn-2}
for I:=2 to Max-1 do
IA[I]:=IA[I-1] + 2 * IA[I-2];
end;
 
 
procedure GetJacobsthalOblong(Max: integer; var IA: TInt64DynArray);
{Jacobsthal Oblong numbers is Nn = Jn x Jn=1 where J = Jacobsthal numbers}
var IA2: TInt64DynArray;
var I: integer;
begin
GetJacobsthalNum(False,Max+1,IA2);
SetLength(IA,Max);
for I:=0 to High(IA2)-1 do
begin
IA[I]:=IA2[I] * IA2[I+1];
end;
end;
 
 
procedure GetJacobsthalPrimes(Memo: TMemo);
var I: integer;
var Jacob,N1, N2: int64;
 
function GetNext: int64;
{Nn = Nn-1 + 2 x Nn-2}
begin
Result:=N1 + 2 * N2;
N2:=N1; N1:=Result;
end;
 
begin
N2:=0; N1:=1;
for I:=1 to 10 do
begin
repeat Jacob:=GetNext;
until IsPrime(Jacob);
Memo.Lines.Add(IntToStr(I)+' - '+IntToStr(Jacob));
end;
end;
 
 
 
procedure ShowJacobsthalNumbers(Memo: TMemo);
var I: integer;
var IA: TInt64DynArray;
var S: string;
begin
GetJacobsthalNum(False,30,IA);
Memo.Lines.Add('First 30 Jacobsthal Numbers');
S:='';
for I:=0 to High(IA) do
begin
S:=S+Format('%12.0n',[IA[I]+0.0]);
if (I mod 5)=4 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
 
Memo.Lines.Add('');
GetJacobsthalNum(True,30,IA);
Memo.Lines.Add('First 30 Jacobsthal-Lucas Numbers');
S:='';
for I:=0 to High(IA) do
begin
S:=S+Format('%14.0n',[IA[I]+0.0]);
if (I mod 4)=3 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
 
Memo.Lines.Add('');
GetJacobsthalOblong(20,IA);
Memo.Lines.Add('First 20 Jacobsthal-Oblong Numbers');
S:='';
for I:=0 to High(IA) do
begin
S:=S+Format('%18.0n',[IA[I]+0.0]);
if (I mod 3)=2 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
 
Memo.Lines.Add('');
GetJacobsthalPrimes(Memo);
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
First 30 Jacobsthal Numbers
0 1 1 3 5
11 21 43 85 171
341 683 1,365 2,731 5,461
10,923 21,845 43,691 87,381 174,763
349,525 699,051 1,398,101 2,796,203 5,592,405
11,184,811 22,369,621 44,739,243 89,478,485 178,956,971
 
 
First 30 Jacobsthal-Lucas Numbers
2 1 5 7
17 31 65 127
257 511 1,025 2,047
4,097 8,191 16,385 32,767
65,537 131,071 262,145 524,287
1,048,577 2,097,151 4,194,305 8,388,607
16,777,217 33,554,431 67,108,865 134,217,727
268,435,457 536,870,911
 
First 20 Jacobsthal-Oblong Numbers
0 1 3
15 55 231
903 3,655 14,535
58,311 232,903 932,295
3,727,815 14,913,991 59,650,503
238,612,935 954,429,895 3,817,763,271
15,270,965,703 61,084,037,575
 
1 - 3
2 - 5
3 - 11
4 - 43
5 - 683
6 - 2731
7 - 43691
8 - 174763
9 - 2796203
10 - 715827883
 
Elapsed Time: 49.627 ms.
 
</pre>
 
=={{header|F Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Jacobsthal numbers: Nigel Galloway January 10th., 2023
let J,JL=let fN g ()=Seq.unfold(fun(n,g)->Some(n,(g,g+2UL*n)))(g,1UL) in (fN 0UL,fN 2UL)
printf "First 30 Jacobsthal are "; J()|>Seq.take 30|>Seq.iter(printf "%d "); printfn ""
printf "First 30 Jacobsthal-Lucas are "; JL()|>Seq.take 30|>Seq.iter(printf "%d "); printfn ""
printf "First 20 Jacobsthal Oblong are "; J()|>Seq.pairwise|>Seq.take 20|>Seq.iter(fun(n,g)->printf "%d " (n*g)); printfn ""
let fN g= Open.Numeric.Primes.MillerRabin.IsPrime &g
printf "First 10 Jacobsthal Primes are "; J()|>Seq.filter fN|>Seq.take 10|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
First 30 Jacobsthal are 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 are 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 are 0 1 3 15 55 231 903 3655 14535 58311 232903 932295 3727815 14913991 59650503 238612935 954429895 3817763271 15270965703 61084037575
First 10 Jacobsthal Primes are 3 5 11 43 683 2731 43691 174763 2796203 715827883
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: grouping io kernel lists lists.lazy math math.functions
math.primes prettyprint sequences ;
 
Line 540 ⟶ 1,592:
 
"First 20 Jacobsthal primes:" print
20 prime-jacobsthals ltake [ . ] leach</langsyntaxhighlight>
{{out}}
<pre>
Line 589 ⟶ 1,641:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Function isPrime(n As Ulongint) As Boolean
If n < 2 Then Return False
If n Mod 2 = 0 Then Return false
Line 639 ⟶ 1,691:
Swap i0, i1
Loop Until c = 10
Sleep</langsyntaxhighlight>
{{out}}
<pre>First 30 Jacobsthal numbers:
Line 674 ⟶ 1,726:
2796203
715827883</pre>
 
=={{header|Gambas}}==
<syntaxhighlight lang="vbnet">Public n As New Long[2]
 
Public Sub Main()
Dim i0 As Integer = 0, i1 As Integer = 1
Dim j As Integer, c As Integer, P As Integer = 1, Q As Integer = -2
Print "First 30 Jacobsthal numbers:"
c = 0
n[i0] = 0
n[i1] = 1
For j = 0 To 29
c += 1
Print Format$(n[i0], " #########");
If (c Mod 5) Then
Print "";
Else
Print Chr(10);
End If
n[i0] = P * n[i1] - Q * n[i0]
Swap i0, i1
Next
Print "\n\nFirst 30 Jacobsthal-Lucas numbers: "
c = 0
n[i0] = 2
n[i1] = 1
For j = 0 To 29
c += 1
Print Format$(n[i0], " #########");
If (c Mod 5) Then
Print "";
Else
Print Chr(10);
End If
n[i0] = P * n[i1] - Q * n[i0]
Swap i0, i1
Next
Print "\n\nFirst 20 Jacobsthal oblong numbers: "
c = 0
n[i0] = 0
n[i1] = 1
For j = 0 To 19
c += 1
Print Format$(n[i0] * n[i1], " ###########");
If (c Mod 5) Then
Print "";
Else
Print Chr(10);
End If
n[i0] = P * n[i1] - Q * n[i0]
Swap i0, i1
Next
Print "\n\nFirst 10 Jacobsthal primes: "
c = 0
n[i0] = 0
n[i1] = 1
Do
If isPrime(n[i0]) Then
c += 1
Print n[i0]
End If
n[i0] = P * n[i1] - Q * n[i0]
Swap i0, i1
Loop Until c = 10
End
 
Public Sub isPrime(ValorEval As Long) As Boolean
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
If ValorEval Mod 3 = 0 Then Return ValorEval = 3
Dim d As Long = 5
While d * d <= ValorEval
If ValorEval Mod d = 0 Then Return False Else d += 2
Wend
Return True
End Function </syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 740 ⟶ 1,878:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 790 ⟶ 1,928:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">jacobsthal :: [Integer]
jacobsthal = 0 : 1 : zipWith (\x y -> 2 * x + y) jacobsthal (tail jacobsthal)
 
Line 814 ⟶ 1,952:
putStrLn ""
putStrLn "First 10 Jacobsthal primes:"
print $ take 10 $ filter isPrime jacobsthal</langsyntaxhighlight>
{{out}}
<pre>
Line 833 ⟶ 1,971:
or, defined in terms of unfoldr:
 
<langsyntaxhighlight lang="haskell">import Data.List (intercalate, transpose, uncons, unfoldr)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime)
Line 840 ⟶ 1,978:
-------------------- JACOBSTHAL NUMBERS ------------------
 
jacobsthal = jacobsthalish:: [0, 1Integer]
jacobsthal = jacobsthalish (0, 1)
 
jacobsthalish :: [(Integer], Integer) -> [Integer]
jacobsthalish = unfoldr go
where
go xs(a, b) = Just (a, (b, 2 * a + b))
let Just (a, t@(b : _)) = uncons xs
in Just (a, t <> [2 * a + b])
 
--------------------------- TEST -------------------------
Line 860 ⟶ 1,997:
( "Jacobsthal-Lucas numbers",
30,
jacobsthalish [(2, 1])
),
( "Jacobsthal oblong numbers",
Line 883 ⟶ 2,020:
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</langsyntaxhighlight>
{{Out}}
<pre>30 terms of the Jacobsthal sequence:
Line 915 ⟶ 2,052:
Implementation:
 
<langsyntaxhighlight Jlang="j">ja=: 3 %~ 2x&^ - _1x&^ NB. Jacobsthal
jl=: 2x&^ + _1x&^ NB.Jacobsthal-Lucas</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight Jlang="j"> ja i.3 10
0 1 1 3 5 11 21 43 85 171
341 683 1365 2731 5461 10923 21845 43691 87381 174763
Line 932 ⟶ 2,069:
232903 932295 3727815 14913991 59650503 238612935 954429895 3817763271 15270965703 61084037575
ja I.1 p:ja i.32 NB. first ten Jacobsthal primes
3 5 11 43 683 2731 43691 174763 2796203 715827883</langsyntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.math.BigInteger;
 
public final class JacobsthalNumbers {
 
public static void main(String[] aArgs) {
System.out.println("The first 30 Jacobsthal Numbers:");
for ( int i = 0; i < 6; i++ ) {
for ( int k = 0; k < 5; k++ ) {
System.out.print(String.format("%15s", jacobsthalNumber(i * 5 + k)));
}
System.out.println();
}
System.out.println();
System.out.println("The first 30 Jacobsthal-Lucas Numbers:");
for ( int i = 0; i < 6; i++ ) {
for ( int k = 0; k < 5; k++ ) {
System.out.print(String.format("%15s", jacobsthalLucasNumber(i * 5 + k)));
}
System.out.println();
}
System.out.println();
System.out.println("The first 20 Jacobsthal oblong Numbers:");
for ( int i = 0; i < 4; i++ ) {
for ( int k = 0; k < 5; k++ ) {
System.out.print(String.format("%15s", jacobsthalOblongNumber(i * 5 + k)));
}
System.out.println();
}
System.out.println();
System.out.println("The first 10 Jacobsthal Primes:");
for ( int i = 0; i < 10; i++ ) {
System.out.println(jacobsthalPrimeNumber(i));
}
}
private static BigInteger jacobsthalNumber(int aIndex) {
BigInteger value = BigInteger.valueOf(parityValue(aIndex));
return BigInteger.ONE.shiftLeft(aIndex).subtract(value).divide(THREE);
}
private static long jacobsthalLucasNumber(int aIndex) {
return ( 1 << aIndex ) + parityValue(aIndex);
}
private static long jacobsthalOblongNumber(int aIndex) {
long nextJacobsthal = jacobsthalNumber(aIndex + 1).longValueExact();
long result = currentJacobsthal * nextJacobsthal;
currentJacobsthal = nextJacobsthal;
return result;
}
private static long jacobsthalPrimeNumber(int aIndex) {
BigInteger candidate = jacobsthalNumber(latestIndex++);
while ( ! candidate.isProbablePrime(CERTAINTY) ) {
candidate = jacobsthalNumber(latestIndex++);
}
return candidate.longValueExact();
}
private static int parityValue(int aIndex) {
return ( aIndex & 1 ) == 0 ? +1 : -1;
}
private static long currentJacobsthal = 0;
private static int latestIndex = 0;
private static final BigInteger THREE = BigInteger.valueOf(3);
private static final int CERTAINTY = 20;
}
 
</syntaxhighlight>
{{ out }}
<pre>
The 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
 
The 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
 
The 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
 
The first 10 Jacobsthal Primes:
3
5
11
43
683
2731
43691
174763
2796203
715827883
</pre>
 
=={{header|jq}}==
Line 941 ⟶ 2,191:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq"># Split the input array into a stream of arrays
def chunks(n):
def c: .[0:n], (if length > n then .[n:]|c else empty end);
Line 957 ⟶ 2,207:
 
# To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);</langsyntaxhighlight>
 
'''The Tasks'''
<langsyntaxhighlight lang="jq">def jacobsthal:
. as $n
| ( (2|power($n)) - (if ($n%2 == 0) then 1 else -1 end)) | divmod(3)[0];
Line 985 ⟶ 2,235:
;
 
tasks</langsyntaxhighlight>
{{out}}
<pre>
Line 1,025 ⟶ 2,275:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Lazy
using Primes
 
Line 1,049 ⟶ 2,299:
printrows("Fifteen Jacabsthal prime numbers:", collect(take(15, Jprimes)), 40, 1, false)
 
</langsyntaxhighlight>{{out}}
<pre>
Thirty Jacobsthal numbers:
Line 1,089 ⟶ 2,339:
845100400152152934331135470251
56713727820156410577229101238628035243
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[Jacobsthal, JacobsthalLucas, JacobsthalOblong]
Jacobsthal[n_]:=(2^n-(-1)^n)/3
JacobsthalLucas[n_]:=2^n+(-1)^n
JacobsthalOblong[n_]:=Jacobsthal[n]Jacobsthal[n+1]
Jacobsthal[Range[0, 29]]
JacobsthalLucas[Range[0, 29]]
JacobsthalOblong[Range[0, 19]]
n=0;
i=0;
Reap[While[n<20,
If[
PrimeQ[Jacobsthal[i]]
,
Sow[{i,Jacobsthal[i]}];
n++;
];
i++;
]][[2,1]]//Grid
</syntaxhighlight>
{{out}}
<pre>{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}
 
{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}
 
{0, 1, 3, 15, 55, 231, 903, 3655, 14535, 58311, 232903, 932295, 3727815, 14913991, 59650503, 238612935, 954429895, 3817763271, 15270965703, 61084037575}
 
3 3
4 5
5 11
7 43
11 683
13 2731
17 43691
19 174763
23 2796203
31 715827883
43 2932031007403
61 768614336404564651
79 201487636602438195784363
101 845100400152152934331135470251
127 56713727820156410577229101238628035243
167 62357403192785191176690552862561408838653121833643
191 1046183622564446793972631570534611069350392574077339085483
199 267823007376498379256993682056860433753700498963798805883563
313 5562466239377370006237035693149875298444543026970449921737087520370363869220418099018130434731
347 95562442332919646317117537304253622533190207882011713489066201641121786503686867002917439712921903606443</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
jacobstahl(n):=(2^n-(-1)^n)/3$
 
jacobstahl_lucas(n):=2^n+(-1)^n$
 
jacobstahl_oblong(n):=jacobstahl(n)*jacobstahl(n+1)$
 
/* Function that returns a list of the first len jacobstahl primes */
jacobstahl_primes_count(len):=block(
[i:0,count:0,result:[]],
while count<len do (if primep(jacobstahl(i)) then (result:endcons(jacobstahl(i),result),count:count+1),i:i+1),
result)$
 
/* Test cases */
makelist(jacobstahl(i),i,0,29);
makelist(jacobstahl_lucas(i),i,0,29);
makelist(jacobstahl_oblong(i),i,0,19);
jacobstahl_primes_count(10);
</syntaxhighlight>
{{out}}
<pre>
[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]
[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]
[0,1,3,15,55,231,903,3655,14535,58311,232903,932295,3727815,14913991,59650503,238612935,954429895,3817763271,15270965703,61084037575]
 
[3,5,11,43,683,2731,43691,174763,2796203,715827883]
</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}}==
<syntaxhighlight lang="ocaml">let is_prime n =
let rec test x =
x * x > n || n mod x <> 0 && n mod (x + 2) <> 0 && test (x + 6)
in
if n < 5
then n land 2 <> 0
else n land 1 <> 0 && n mod 3 <> 0 && test 5
 
let seq_jacobsthal =
let rec next b a () = Seq.Cons (a, next (a + a + b) b) in
next 1
 
let seq_jacobsthal_oblong =
let rec next b a () = Seq.Cons (a * b, next (a + a + b) b) in
next 1 0
 
let () =
let show (n, seq, s) =
Seq.take n seq
|> Seq.fold_left (Printf.sprintf "%s %u") (Printf.sprintf "First %u %s numbers:\n" n s)
|> print_endline
in
List.iter show [
30, seq_jacobsthal 0, "Jacobsthal";
30, seq_jacobsthal 2, "Jacobsthal-Lucas";
20, seq_jacobsthal_oblong, "Jacobsthal oblong";
10, Seq.filter is_prime (seq_jacobsthal 0), "Jacobsthal prime"]</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|PARI/GP}}==
<syntaxhighlight lang="PARI/GP">
\\ Define the Jacobsthal function
Jacobsthal(n) = (2^n - (-1)^n) / 3;
 
\\ Define the JacobsthalLucas function
JacobsthalLucas(n) = 2^n + (-1)^n;
 
\\ Define the JacobsthalOblong function
JacobsthalOblong(n) = Jacobsthal(n) * Jacobsthal(n + 1);
 
 
{
\\ Generate and print Jacobsthal numbers for 0 through 29
print(vector(30, n, Jacobsthal(n-1)));
 
\\ Generate and print JacobsthalLucas numbers for 0 through 29
print(vector(30, n, JacobsthalLucas(n-1)));
 
\\ Generate and print JacobsthalOblong numbers for 0 through 19
print(vector(20, n, JacobsthalOblong(n-1)));
 
\\ Find the first 20 prime numbers in the Jacobsthal sequence
myprimes = [];
i = 0;
while(#myprimes < 40,
if(isprime(Jacobsthal(i)), myprimes = concat(myprimes, [i, Jacobsthal(i)]));
i++;
);
 
for (i = 1, #myprimes\2, print(myprimes[2*i-1] " " myprimes[2*i]); );
}
</syntaxhighlight>
{{out}}
<pre>
[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]
[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]
[0, 1, 3, 15, 55, 231, 903, 3655, 14535, 58311, 232903, 932295, 3727815, 14913991, 59650503, 238612935, 954429895, 3817763271, 15270965703, 61084037575]
3 3
4 5
5 11
7 43
11 683
13 2731
17 43691
19 174763
23 2796203
31 715827883
43 2932031007403
61 768614336404564651
79 201487636602438195784363
101 845100400152152934331135470251
127 56713727820156410577229101238628035243
167 62357403192785191176690552862561408838653121833643
191 1046183622564446793972631570534611069350392574077339085483
199 267823007376498379256993682056860433753700498963798805883563
313 5562466239377370006237035693149875298444543026970449921737087520370363869220418099018130434731
347 95562442332919646317117537304253622533190207882011713489066201641121786503686867002917439712921903606443
 
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <say state>;
Line 1,112 ⟶ 2,642:
say "First 30 Jacobsthal-Lucas numbers:\n", table map { jacobsthal_lucas $_-1 } 1..30;
say "First 20 Jacobsthal oblong numbers:\n", table map { $j[$_-1] * $j[$_] } 1..20;
say "First 20 Jacobsthal primes:\n", join "\n", @jp;</langsyntaxhighlight>
{{out}}
<pre>First 30 Jacobsthal numbers:
Line 1,160 ⟶ 2,690:
=={{header|Phix}}==
You can run this online [http://phix.x10.mx/p2js/jacobsthal.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">jacobsthal</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 1,174 ⟶ 2,704:
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8008080;">printffunction</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 30 Jacobsthal numbers:\n%s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">join_byjba</span><span style="color: #0000FF;">(</span><span style="color: #7060A8004080;">applystring</span><span style="color: #0000FF;">(</span><span style="color: #004600000000;">truefmt</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF004080;">,{{</span><span style="color: #008000;">"%9d"sequence</span> <span style="color: #0000FF;">},</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">29s</span><span style="color: #0000FF;">,</span> <span style="color: #000000004080;">0integer</span><span style="color: #0000FF;">),</span><span style="color: #000000;">jacobsthalb</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF008080;">,</span><span style="color: #008000;">"First 30 Jacobsthal-Lucas numbers:\n%s\n"</span><span style="color: #0000FF;">,return</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000000000;">"%9d"fmt</span> <span style="color: #0000FF;">},</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">29s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">jacobsthal_lucas</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5b</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 20 Jacobsthal oblong numbers:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%11d"</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">jacobsthal_oblong</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 30 Jacobsthal numbers:\n%s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">jba</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%9d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">jacobsthal</span><span style="color: #0000FF;">)))</span>
<span style="color: #000080;font-style:italic;">--printf(1,"First 10 Jacobsthal primes:\n%s\n", {join(apply(true,sprintf,{{"%d"},filter(apply(tagset(31,0),jacobsthal),is_prime)}),"\n")})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 30 Jacobsthal-Lucas numbers:\n%s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">jba</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%9d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">jacobsthal_lucas</span><span style="color: #0000FF;">)))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 20 Jacobsthal oblong numbers:\n%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">jba</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%11d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">jacobsthal_oblong</span><span style="color: #0000FF;">)))</span>
<span style="color: #000080;font-style:italic;">--printf(1,"First 10 Jacobsthal primes:\n%s\n", jba("%d",filter(apply(tagset(31,0),jacobsthal),is_prime),1))
--hmm(""), fine, but to go further roll out gmp:</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,193 ⟶ 2,726:
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
Likewise should you want the three basic functions to go further they'll have to look much more like the C submission above.
{{out}}
Line 1,244 ⟶ 2,777:
=={{header|Python}}==
{{trans|Phix}}
<langsyntaxhighlight lang="python">#!/usr/bin/python
from math import floor, pow
 
Line 1,282 ⟶ 2,815:
for j in range(3, 33):
if isPrime(jacobsthal(j)):
print(jacobsthal(j))</langsyntaxhighlight>
 
{{out}}
Line 1,307 ⟶ 2,840:
 
 
Or, defineddefining an infinite series in terms of a general unfoldr anamorphism:
 
<langsyntaxhighlight lang="python">'''Jacobsthal numbers'''
 
from itertools import islice
Line 1,319 ⟶ 2,852:
'''Infinite sequence of terms of OEIS A001045
'''
return jacobsthalish([0, 1])
 
 
# jacobsthalish :: (Int, Int) -> [Int]
def jacobsthalish(xs*xy):
'''Infinite sequence of jacobsthal-type series
beginning with a, b
'''
def go(ysab):
a, *tb = ysab
return a, t(b, + [2 * a + t[0]]b)
 
return unfoldr(go)(xsxy)
 
 
Line 1,347 ⟶ 2,880:
(
'Jacobsthal-Lucas numbers',
30, jacobsthalish([2, 1])
),
(
Line 1,477 ⟶ 3,010:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>30 terms of the Jacobsthal sequence:
Line 1,504 ⟶ 3,037:
3 5 11 43 683
2731 43691 174763 2796203 715827883</pre>
 
=={{header|Quackery}}==
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ 2 over ** -1 rot ** - 3 / ] is j ( n --> n )
 
[ 2 over ** -1 rot ** + ] is jl ( n --> n )
 
[ dup 1+ j swap j * ] is jo ( n --> n )
 
say "First 30 Jacobsthal numbers:"
cr
30 times [ i^ j echo sp ]
cr cr
say "First 30 Jacobsthal-Lucas numbers:"
cr
30 times [ i^ jl echo sp ]
cr cr
say "First 20 Jacobsthal oblong numbers:"
cr
20 times [ i^ jo echo sp ]
cr cr
say "First 10 Jacobsthal primes:"
cr
[] 0
[ dup j dup isprime iff
[ swap dip join ]
else drop
1+
over size 10 = until ]
drop
witheach [ echo sp ]</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 primes:
3 5 11 43 683 2731 43691 174763 2796203 715827883
</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my $jacobsthal = cache lazy 0, 1, * × 2 + * … *;
my $jacobsthal-lucas = lazy 2, 1, * × 2 + * … *;
 
Line 1,519 ⟶ 3,100:
 
say "\nFirst 20 Jacobsthal primes:";
say $jacobsthal.grep( &is-prime )[^20].join: "\n";</langsyntaxhighlight>
{{out}}
<pre>First 30 Jacobsthal numbers:
Line 1,566 ⟶ 3,147:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red ["Jacobsthal numbers"]
 
jacobsthal: function [n] [to-integer (2 ** n - (-1 ** n) / 3)]
Line 1,617 ⟶ 3,198:
]
n: n + 1
]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,653 ⟶ 3,234:
2796203
715827883
</pre>
 
=={{header|RPL}}==
===Straightforward approach===
The Jacobstahl pieces of code are macros more than programs.
≪ 2 OVER ^ -1 ROT ^ - 3 / ≫
'JCBN' STO
≪ 2 OVER ^ -1 ROT ^ + ≫
'JCBL' STO
≪ DUP JCBN SWAP 1 + JCBN * ≫
'JCBO' STO
The primality test is the only one that deserves such a name:
≪ IF DUP 5 ≤ THEN
{ 2 3 5 } SWAP POS
ELSE
IF DUP 2 MOD NOT THEN
2
ELSE
DUP √ CEIL → lim
≪ 3
WHILE DUP2 MOD OVER lim ≤ AND REPEAT 2 + END
END
MOD
END
SIGN
'PRIM?' STO
===Using binary integers===
The task is an opportunity to showcase the use of binary integers in RPL, but it's actually slower and fatter, as many RPL instructions are designed for floating point numbers only.
≪ → n
≪ # 1h 1 n START SL NEXT
IF n R→B # 1h AND B→R THEN 1 + ELSE 1 - END
3 / B→R
≫ ≫
'JCBN' STO
===Testing program===
≪ → func count
≪ { } 0
'''DO''' DUP func
'''IF''' EVAL '''THEN''' ROT SWAP + SWAP '''ELSE''' DROP '''END'''
1 +
'''UNTIL''' OVER SIZE count ≥ '''END'''
DROP
≫ ≫
'<span style="color:blue">ASSRT</span>' STO
 
≪ <span style="color:blue">JCBN</span> 1 ≫ 30 <span style="color:blue">ASSRT</span>
≪ <span style="color:blue">JCBL</span> 1 ≫ 30 <span style="color:blue">ASSRT</span>
≪ <span style="color:blue">JCBO </span>1 ≫ 20 <span style="color:blue">ASSRT</span>
≪ DUP <span style="color:blue">JCBN PRIM?</span> ≫ 10 <span style="color:blue">ASSRT</span>
{{works with|Halcyon Calc|4.2.7}}
{{out}}
<pre>
4: { 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 }
3: { 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 }
2: { 0 1 3 15 55 231 903 3655 14535 58311 232903 932295 3727815 14913991 59650503 238612935 954429895 3817763271 15270965703 61084037575 }
1: { 3 5 11 43 683 2731 43691 174763 2796203 715827883 }
</pre>
 
=={{header|Ruby}}==
Since version 3.0, Ruby supports "end-less" method definitions.
<syntaxhighlight lang="ruby">require 'prime'
 
def jacobsthal(n) = (2**n + n[0])/3
def jacobsthal_lucas(n) = 2**n + (-1)**n
def jacobsthal_oblong(n) = jacobsthal(n) * jacobsthal(n+1)
 
puts "First 30 Jacobsthal numbers:"
puts (0..29).map{|n| jacobsthal(n) }.join(" ")
 
puts "\nFirst 30 Jacobsthal-Lucas numbers: "
puts (0..29).map{|n| jacobsthal_lucas(n) }.join(" ")
 
puts "\nFirst 20 Jacobsthal-Oblong numbers: "
puts (0..19).map{|n| jacobsthal_oblong(n) }.join(" ")
 
puts "\nFirst 10 prime Jacobsthal numbers: "
res = (0..).lazy.filter_map do |i|
j = jacobsthal(i)
j if j.prime?
end
puts res.take(10).force.join(" ")
</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 prime Jacobsthal numbers:
3 5 11 43 683 2731 43691 174763 2796203 715827883
</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// rug = "0.3"
 
Line 1,702 ⟶ 3,381:
println!("{}", n);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,750 ⟶ 3,429:
95562442332919646317117537304253622533190207882011713489066201641121786503686867002917439712921903606443
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program jacobsthal_numbers;
print("First 30 Jacobsthal numbers:");
printseq([j n : n in [0..29]]);
print;
 
print("First 30 Jacobsthal-Lucas numbers:");
printseq([jl n : n in [0..29]]);
print;
 
print("First 20 Jacobsthal oblong numbers:");
printseq([jo n : n in [0..19]]);
print;
 
print("First 10 Jacobsthal primes:");
printseq([j n : n in [0..31] | prime j n]);
 
proc printseq(seq);
loop for n in seq do
nprint(lpad(str n, 14));
if (i +:= 1) mod 5 = 0 then print; end if;
end loop;
end proc;
 
op j(n);
return (2**n - (-1)**n) div 3;
end op;
 
op jl(n);
return 2**n + (-1)**n;
end op;
 
op jo(n);
return j n * j (n+1);
end op;
 
op prime(n);
if n<=4 then return n in {2,3}; end if;
return not exists d in [2..floor sqrt n] | n mod d = 0;
end op;
end program;</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 primes:
3 5 11 43 683
2731 43691 174763 2796203 715827883</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func jacobsthal(n) {
lucasU(1, -2, n)
}
Line 1,770 ⟶ 3,517:
 
say "\nFirst 20 Jacobsthal primes:";
say (1..Inf -> lazy.map(jacobsthal).grep{.is_prime}.first(20))</langsyntaxhighlight>
 
{{out}}
Line 1,785 ⟶ 3,532:
First 20 Jacobsthal primes:
[3, 5, 11, 43, 683, 2731, 43691, 174763, 2796203, 715827883, 2932031007403, 768614336404564651, 201487636602438195784363, 845100400152152934331135470251, 56713727820156410577229101238628035243, 62357403192785191176690552862561408838653121833643, 1046183622564446793972631570534611069350392574077339085483, 267823007376498379256993682056860433753700498963798805883563, 5562466239377370006237035693149875298444543026970449921737087520370363869220418099018130434731, 95562442332919646317117537304253622533190207882011713489066201641121786503686867002917439712921903606443]
</pre>
 
=={{header|uBasic/4tH}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="uBasic/4tH">Dim @n(2)
x = 0
y = 1
p = 1
q = -2
 
Print "First 30 Jacobsthal numbers:"
c = 0 : @n(x) = 0: @n(y) = 1
For j = 0 To 29
c = c + 1
Print Using " ____________#"; @n(x);
If (c % 5) = 0 Then Print
@n(x) = P * @n(y) - Q * @n(x)
Push x : x = y : y = Pop()
Next
 
Print : Print "First 30 Jacobsthal-Lucas numbers: "
c = 0 : @n(x) = 2: @n(y) = 1
For j = 0 To 29
c = c + 1
Print Using " ____________#"; @n(x);
If (c % 5) = 0 Then Print
@n(x) = P * @n(y) - Q * @n(x)
Push x : x = y : y = Pop()
Next
 
Print : Print "First 20 Jacobsthal oblong numbers: "
c = 0 : @n(x) = 0: @n(y) = 1
For j = 0 To 19
c = c + 1
Print Using " ____________#"; @n(x)*@n(y);
If (c % 5) = 0 Then Print
@n(x) = P * @n(y) - Q * @n(x)
Push x : x = y : y = Pop()
Next
 
Print : Print "First 10 Jacobsthal primes: "
c = 0 : @n(x) = 0 : @n(y) = 1
Do
If FUNC(_isPrime(@n(x))) Then c = c + 1 : Print @n(x)
@n(x) = P * @n(y) - Q * @n(x)
Push x : x = y : y = Pop()
Until c = 10
Loop
 
End
 
_isPrime
Param (1)
Local (1)
 
If (a@ < 2) Then Return (0)
If (a@ % 2) = 0 Then Return (0)
For b@ = 3 To Func(_Sqrt(a@, 0))+1 Step 2
If (a@ % b@) = 0 Then Unloop : Return (0)
Next
Return (1)
 
_Sqrt
Param (2)
Local (2)
 
If a@ = 0 Return (0)
c@ = Max(Shl(Set(a@, a@*(10^(b@*2))), -10), 1024)
 
Do
d@ = (c@+a@/c@)/2
While (c@ > d@)
c@ = d@
Loop
Return (c@)</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 primes:
3
5
11
43
683
2731
43691
174763
2796203
715827883
 
0 OK, 0:1024 </pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
{{incomplete|V (Vlang)|Probably Prime section isn't implemented yet (This is in development)}}
<syntaxhighlight lang="v (vlang)">import math.big
 
fn jacobsthal(n u32) big.Integer {
mut t := big.one_int
t=t.lshift(n)
mut s := big.one_int
if n%2 != 0 {
s=s.neg()
}
t -= s
return t/big.integer_from_int(3)
}
fn jacobsthal_lucas(n u32) big.Integer {
mut t := big.one_int
t=t.lshift(n)
mut a := big.one_int
if n%2 != 0 {
a=a.neg()
}
return t+a
}
fn main() {
mut jac := []big.Integer{len: 30}
println("First 30 Jacobsthal numbers:")
for i := u32(0); i < 30; i++ {
jac[i] = jacobsthal(i)
print("${jac[i]:9} ")
if (i+1)%5 == 0 {
println('')
}
}
println("\nFirst 30 Jacobsthal-Lucas numbers:")
for i := u32(0); i < 30; i++ {
print("${jacobsthal_lucas(i):9} ")
if (i+1)%5 == 0 {
println('')
}
}
println("\nFirst 20 Jacobsthal oblong numbers:")
for i := u32(0); i < 20; i++ {
print("${jac[i]*jac[i+1]:11} ")
if (i+1)%5 == 0 {
println('')
}
}
/*println("\nFirst 20 Jacobsthal primes:")
for n, count := u32(0), 0; count < 20; n++ {
j := jacobsthal(n)
if j.probably_prime(10) {
println(j)
count++
}
}*/
}</syntaxhighlight>
 
{{out}}
<pre>
First 30 Jacobsthal numbers:
0 1 1 3 5
11 21 43 85 171
341 683 1,365 2,731 5,461
10,923 21,845 43,691 87,381 174,763
349,525 699,051 1,398,101 2,796,203 5,592,405
11,184,811 22,369,621 44,739,243 89,478,485 178,956,971
 
First 30 Jacobsthal-Lucas numbers:
2 1 5 7 17
31 65 127 257 511
1,025 2,047 4,097 8,191 16,385
32,767 65,537 131,071 262,145 524,287
1,048,577 2,097,151 4,194,305 8,388,607 16,777,217
33,554,431 67,108,865 134,217,727 268,435,457 536,870,911
 
First 20 Jacobsthal oblong numbers:
0 1 3 15 55
231 903 3,655 14,535 58,311
232,903 932,295 3,727,815 14,913,991 59,650,503
238,612,935 954,429,895 3,817,763,271 15,270,965,703 61,084,037,575
 
</pre>
 
Line 1,791 ⟶ 3,737:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt
import "./seq" for Lst
import "./fmt" for Fmt
Line 1,801 ⟶ 3,747:
System.print("First 30 Jacobsthal numbers:")
var js = (0..29).map { |i| jacobsthal.call(i) }.toList
for (chunk in Lst.chunks(js, 5)) Fmt.printtprint("$,12i", chunkjs, 5)
 
System.print("\nFirst 30 Jacobsthal-Lucas numbers:")
var jsl = (0..29).map { |i| jacobsthalLucas.call(i) }.toList
for (chunk in Lst.chunks(jsl, 5)) Fmt.printtprint("$,12i", chunkjsl, 5)
 
System.print("\nFirst 20 Jacobsthal oblong numbers:")
var oblongs = (0..19).map { |i| js[i] * js[i+1] }.toList
for (chunk in Lst.chunks(oblongs, 5)) Fmt.printtprint("$,14i", chunkoblongs, 5)
 
var primes = js.where { |j| j.isProbablePrime(10) }.toList
Line 1,823 ⟶ 3,769:
}
System.print("\nFirst 20 Jacobsthal primes:")
for (i in 0..19) Fmt.print("$i", primes[i])</langsyntaxhighlight>
 
{{out}}
Line 1,873 ⟶ 3,819:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 1,926 ⟶ 3,872:
J2:= J1; J1:= J;
];
]</langsyntaxhighlight>
 
{{out}}
337

edits