Jacobsthal numbers: Difference between revisions

Content added Content deleted
(Added AutoHotkey)
m (syntax highlighting fixup automation)
Line 53: Line 53:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find some Jacobsthal and related Numbers #
<syntaxhighlight lang="algol68">BEGIN # find some Jacobsthal and related Numbers #
INT max jacobsthal = 29; # highest Jacobsthal number we will find #
INT max jacobsthal = 29; # highest Jacobsthal number we will find #
INT max oblong = 20; # highest Jacobsthal oblong number we will find #
INT max oblong = 20; # highest Jacobsthal oblong number we will find #
Line 103: Line 103:
FI
FI
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 151: Line 151:
=={{header|AppleScript}}==
=={{header|AppleScript}}==
===Procedural===
===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.
-- variant: text containing "Lucas", "oblong", or "prime" — or none of these.
-- n: length of output sequence required.
-- n: length of output sequence required.
Line 284: Line 284:


task()
task()
</syntaxhighlight>
</lang>


{{output}}
{{output}}
<lang applescript>"First 30 Jacobsthal Numbers:
<syntaxhighlight lang="applescript">"First 30 Jacobsthal Numbers:
0 1 1 3 5 11 21 43 85 171
0 1 1 3 5 11 21 43 85 171
341 683 1365 2731 5461 10923 21845 43691 87381 174763
341 683 1365 2731 5461 10923 21845 43691 87381 174763
Line 303: Line 303:
First 11 Jacobsthal Primes:
First 11 Jacobsthal Primes:
3 5 11 43 683 2731
3 5 11 43 683 2731
43691 174763 2796203 715827883 2932031007403"</lang>
43691 174763 2796203 715827883 2932031007403"</syntaxhighlight>


===Functional===
===Functional===
<lang applescript>-------------------- JACOBSTHAL NUMBERS ------------------
<syntaxhighlight lang="applescript">-------------------- JACOBSTHAL NUMBERS ------------------


-- e.g. take(10, jacobsthal())
-- e.g. take(10, jacobsthal())
Line 741: Line 741:
end |λ|
end |λ|
end script
end script
end zipWith</lang>
end zipWith</syntaxhighlight>
{{Out}}
{{Out}}
<pre>30 first terms of the Jacobsthal sequence:
<pre>30 first terms of the Jacobsthal sequence:
Line 771: Line 771:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>J: function [n]-> ((2^n) - (neg 1)^n)/3
<syntaxhighlight lang="rebol">J: function [n]-> ((2^n) - (neg 1)^n)/3
JL: function [n]-> (2^n) + (neg 1)^n
JL: function [n]-> (2^n) + (neg 1)^n
JO: function [n]-> (J n) * (J n+1)
JO: function [n]-> (J n) * (J n+1)
Line 798: Line 798:
printFirst "Jacobsthal-Lucas numbers" 'JL [true] 30
printFirst "Jacobsthal-Lucas numbers" 'JL [true] 30
printFirst "Jacobsthal oblong numbers" 'JO [true] 20
printFirst "Jacobsthal oblong numbers" 'JO [true] 20
printFirst "Jacobsthal primes" 'J [prime? num] 20</lang>
printFirst "Jacobsthal primes" 'J [prime? num] 20</syntaxhighlight>


{{out}}
{{out}}
Line 847: Line 847:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>Jacobsthal(n){
<syntaxhighlight lang="autohotkey">Jacobsthal(n){
return SubStr(" " Format("{:.0f}", (2**n - (-1)**n ) / 3), -8)
return SubStr(" " Format("{:.0f}", (2**n - (-1)**n ) / 3), -8)
}
}
Line 881: Line 881:
ans.push(n)
ans.push(n)
return ans
return ans
}</lang>
}</syntaxhighlight>
Examples:<lang AutoHotkey>result := "First 30 Jacobsthal numbers:`n"
Examples:<syntaxhighlight lang="autohotkey">result := "First 30 Jacobsthal numbers:`n"
loop 30
loop 30
result .= Jacobsthal(A_Index-1) (mod(A_Index, 5) ? " ":"`n")
result .= Jacobsthal(A_Index-1) (mod(A_Index, 5) ? " ":"`n")
Line 901: Line 901:


MsgBox, 262144, , % result
MsgBox, 262144, , % result
return</lang>
return</syntaxhighlight>
{{out}}
{{out}}
<pre>First 30 Jacobsthal numbers:
<pre>First 30 Jacobsthal numbers:
Line 931: Line 931:
=={{header|C}}==
=={{header|C}}==
{{libheader|GMP}}
{{libheader|GMP}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <gmp.h>
#include <gmp.h>


Line 991: Line 991:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,042: Line 1,042:
=={{header|C++}}==
=={{header|C++}}==
{{libheader|GMP}}
{{libheader|GMP}}
<lang cpp>#include <gmpxx.h>
<syntaxhighlight lang="cpp">#include <gmpxx.h>


#include <iomanip>
#include <iomanip>
Line 1,089: Line 1,089:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,140: Line 1,140:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: grouping io kernel lists lists.lazy math math.functions
<syntaxhighlight lang="factor">USING: grouping io kernel lists lists.lazy math math.functions
math.primes prettyprint sequences ;
math.primes prettyprint sequences ;


Line 1,165: Line 1,165:


"First 20 Jacobsthal primes:" print
"First 20 Jacobsthal primes:" print
20 prime-jacobsthals ltake [ . ] leach</lang>
20 prime-jacobsthals ltake [ . ] leach</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,214: Line 1,214:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>Function isPrime(n As Ulongint) As Boolean
<syntaxhighlight lang="freebasic">Function isPrime(n As Ulongint) As Boolean
If n < 2 Then Return False
If n < 2 Then Return False
If n Mod 2 = 0 Then Return false
If n Mod 2 = 0 Then Return false
Line 1,264: Line 1,264:
Swap i0, i1
Swap i0, i1
Loop Until c = 10
Loop Until c = 10
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>First 30 Jacobsthal numbers:
<pre>First 30 Jacobsthal numbers:
Line 1,301: Line 1,301:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,365: Line 1,365:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,415: Line 1,415:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>jacobsthal :: [Integer]
<syntaxhighlight lang="haskell">jacobsthal :: [Integer]
jacobsthal = 0 : 1 : zipWith (\x y -> 2 * x + y) jacobsthal (tail jacobsthal)
jacobsthal = 0 : 1 : zipWith (\x y -> 2 * x + y) jacobsthal (tail jacobsthal)


Line 1,439: Line 1,439:
putStrLn ""
putStrLn ""
putStrLn "First 10 Jacobsthal primes:"
putStrLn "First 10 Jacobsthal primes:"
print $ take 10 $ filter isPrime jacobsthal</lang>
print $ take 10 $ filter isPrime jacobsthal</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,458: Line 1,458:
or, defined in terms of unfoldr:
or, defined in terms of unfoldr:


<lang haskell>import Data.List (intercalate, transpose, uncons, unfoldr)
<syntaxhighlight lang="haskell">import Data.List (intercalate, transpose, uncons, unfoldr)
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime)
import Data.Numbers.Primes (isPrime)
Line 1,507: Line 1,507:
let ws = maximum . fmap length <$> transpose rows
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</lang>
in unlines $ intercalate gap . zipWith pw ws <$> rows</syntaxhighlight>
{{Out}}
{{Out}}
<pre>30 terms of the Jacobsthal sequence:
<pre>30 terms of the Jacobsthal sequence:
Line 1,539: Line 1,539:
Implementation:
Implementation:


<lang J>ja=: 3 %~ 2x&^ - _1x&^ NB. Jacobsthal
<syntaxhighlight lang="j">ja=: 3 %~ 2x&^ - _1x&^ NB. Jacobsthal
jl=: 2x&^ + _1x&^ NB.Jacobsthal-Lucas</lang>
jl=: 2x&^ + _1x&^ NB.Jacobsthal-Lucas</syntaxhighlight>


Task examples:
Task examples:


<lang J> ja i.3 10
<syntaxhighlight lang="j"> ja i.3 10
0 1 1 3 5 11 21 43 85 171
0 1 1 3 5 11 21 43 85 171
341 683 1365 2731 5461 10923 21845 43691 87381 174763
341 683 1365 2731 5461 10923 21845 43691 87381 174763
Line 1,556: Line 1,556:
232903 932295 3727815 14913991 59650503 238612935 954429895 3817763271 15270965703 61084037575
232903 932295 3727815 14913991 59650503 238612935 954429895 3817763271 15270965703 61084037575
ja I.1 p:ja i.32 NB. first ten Jacobsthal primes
ja I.1 p:ja i.32 NB. first ten Jacobsthal primes
3 5 11 43 683 2731 43691 174763 2796203 715827883</lang>
3 5 11 43 683 2731 43691 174763 2796203 715827883</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Line 1,565: Line 1,565:


'''Preliminaries'''
'''Preliminaries'''
<lang jq># Split the input array into a stream of arrays
<syntaxhighlight lang="jq"># Split the input array into a stream of arrays
def chunks(n):
def chunks(n):
def c: .[0:n], (if length > n then .[n:]|c else empty end);
def c: .[0:n], (if length > n then .[n:]|c else empty end);
Line 1,581: Line 1,581:


# To take advantage of gojq's arbitrary-precision integer arithmetic:
# To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);</lang>
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);</syntaxhighlight>


'''The Tasks'''
'''The Tasks'''
<lang jq>def jacobsthal:
<syntaxhighlight lang="jq">def jacobsthal:
. as $n
. as $n
| ( (2|power($n)) - (if ($n%2 == 0) then 1 else -1 end)) | divmod(3)[0];
| ( (2|power($n)) - (if ($n%2 == 0) then 1 else -1 end)) | divmod(3)[0];
Line 1,609: Line 1,609:
;
;


tasks</lang>
tasks</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,649: Line 1,649:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Lazy
<syntaxhighlight lang="julia">using Lazy
using Primes
using Primes


Line 1,673: Line 1,673:
printrows("Fifteen Jacabsthal prime numbers:", collect(take(15, Jprimes)), 40, 1, false)
printrows("Fifteen Jacabsthal prime numbers:", collect(take(15, Jprimes)), 40, 1, false)


</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Thirty Jacobsthal numbers:
Thirty Jacobsthal numbers:
Line 1,716: Line 1,716:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[Jacobsthal, JacobsthalLucas, JacobsthalOblong]
<syntaxhighlight lang="mathematica">ClearAll[Jacobsthal, JacobsthalLucas, JacobsthalOblong]
Jacobsthal[n_]:=(2^n-(-1)^n)/3
Jacobsthal[n_]:=(2^n-(-1)^n)/3
JacobsthalLucas[n_]:=2^n+(-1)^n
JacobsthalLucas[n_]:=2^n+(-1)^n
Line 1,734: Line 1,734:
i++;
i++;
]][[2,1]]//Grid
]][[2,1]]//Grid
</syntaxhighlight>
</lang>
{{out}}
{{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}
<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}
Line 1,765: Line 1,765:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature <say state>;
use feature <say state>;
Line 1,784: Line 1,784:
say "First 30 Jacobsthal-Lucas numbers:\n", table map { jacobsthal_lucas $_-1 } 1..30;
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 oblong numbers:\n", table map { $j[$_-1] * $j[$_] } 1..20;
say "First 20 Jacobsthal primes:\n", join "\n", @jp;</lang>
say "First 20 Jacobsthal primes:\n", join "\n", @jp;</syntaxhighlight>
{{out}}
{{out}}
<pre>First 30 Jacobsthal numbers:
<pre>First 30 Jacobsthal numbers:
Line 1,832: Line 1,832:
=={{header|Phix}}==
=={{header|Phix}}==
You can run this online [http://phix.x10.mx/p2js/jacobsthal.htm here].
You can run this online [http://phix.x10.mx/p2js/jacobsthal.htm here].
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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,865: Line 1,865:
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
Likewise should you want the three basic functions to go further they'll have to look much more like the C submission above.
Likewise should you want the three basic functions to go further they'll have to look much more like the C submission above.
{{out}}
{{out}}
Line 1,916: Line 1,916:
=={{header|Python}}==
=={{header|Python}}==
{{trans|Phix}}
{{trans|Phix}}
<lang python>#!/usr/bin/python
<syntaxhighlight lang="python">#!/usr/bin/python
from math import floor, pow
from math import floor, pow


Line 1,954: Line 1,954:
for j in range(3, 33):
for j in range(3, 33):
if isPrime(jacobsthal(j)):
if isPrime(jacobsthal(j)):
print(jacobsthal(j))</lang>
print(jacobsthal(j))</syntaxhighlight>


{{out}}
{{out}}
Line 1,981: Line 1,981:
Or, defining an infinite series in terms of a general unfoldr anamorphism:
Or, defining an infinite series in terms of a general unfoldr anamorphism:


<lang python>'''Jacobsthal numbers'''
<syntaxhighlight lang="python">'''Jacobsthal numbers'''


from itertools import islice
from itertools import islice
Line 2,149: Line 2,149:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>30 terms of the Jacobsthal sequence:
<pre>30 terms of the Jacobsthal sequence:
Line 2,178: Line 2,178:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>my $jacobsthal = cache lazy 0, 1, * × 2 + * … *;
<syntaxhighlight lang="raku" line>my $jacobsthal = cache lazy 0, 1, * × 2 + * … *;
my $jacobsthal-lucas = lazy 2, 1, * × 2 + * … *;
my $jacobsthal-lucas = lazy 2, 1, * × 2 + * … *;


Line 2,191: Line 2,191:


say "\nFirst 20 Jacobsthal primes:";
say "\nFirst 20 Jacobsthal primes:";
say $jacobsthal.grep( &is-prime )[^20].join: "\n";</lang>
say $jacobsthal.grep( &is-prime )[^20].join: "\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>First 30 Jacobsthal numbers:
<pre>First 30 Jacobsthal numbers:
Line 2,238: Line 2,238:


=={{header|Red}}==
=={{header|Red}}==
<lang rebol>Red ["Jacobsthal numbers"]
<syntaxhighlight lang="rebol">Red ["Jacobsthal numbers"]


jacobsthal: function [n] [to-integer (2 ** n - (-1 ** n) / 3)]
jacobsthal: function [n] [to-integer (2 ** n - (-1 ** n) / 3)]
Line 2,289: Line 2,289:
]
]
n: n + 1
n: n + 1
]</lang>
]</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,328: Line 2,328:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>// [dependencies]
<syntaxhighlight lang="rust">// [dependencies]
// rug = "0.3"
// rug = "0.3"


Line 2,374: Line 2,374:
println!("{}", n);
println!("{}", n);
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,424: Line 2,424:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func jacobsthal(n) {
<syntaxhighlight lang="ruby">func jacobsthal(n) {
lucasU(1, -2, n)
lucasU(1, -2, n)
}
}
Line 2,442: Line 2,442:


say "\nFirst 20 Jacobsthal primes:";
say "\nFirst 20 Jacobsthal primes:";
say (1..Inf -> lazy.map(jacobsthal).grep{.is_prime}.first(20))</lang>
say (1..Inf -> lazy.map(jacobsthal).grep{.is_prime}.first(20))</syntaxhighlight>


{{out}}
{{out}}
Line 2,462: Line 2,462:
{{trans|go}}
{{trans|go}}
{{incomplete|Vlang|Probably Prime section isn't implemented yet (This is in development)}}
{{incomplete|Vlang|Probably Prime section isn't implemented yet (This is in development)}}
<lang vlang>import math.big
<syntaxhighlight lang="vlang">import math.big


fn jacobsthal(n u32) big.Integer {
fn jacobsthal(n u32) big.Integer {
Line 2,520: Line 2,520:
}
}
}*/
}*/
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,552: Line 2,552:
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "./big" for BigInt
<syntaxhighlight lang="ecmascript">import "./big" for BigInt
import "./seq" for Lst
import "./seq" for Lst
import "./fmt" for Fmt
import "./fmt" for Fmt
Line 2,584: Line 2,584:
}
}
System.print("\nFirst 20 Jacobsthal primes:")
System.print("\nFirst 20 Jacobsthal primes:")
for (i in 0..19) Fmt.print("$i", primes[i])</lang>
for (i in 0..19) Fmt.print("$i", primes[i])</syntaxhighlight>


{{out}}
{{out}}
Line 2,634: Line 2,634:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is prime
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
int N, I;
[if N <= 2 then return N = 2;
[if N <= 2 then return N = 2;
Line 2,687: Line 2,687:
J2:= J1; J1:= J;
J2:= J1; J1:= J;
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}