Jacobsthal numbers: Difference between revisions
m
syntax highlighting fixup automation
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 53:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{libheader|ALGOL 68-primes}}
<
INT max jacobsthal = 29; # highest Jacobsthal number we will find #
INT max oblong = 20; # highest Jacobsthal oblong number we will find #
Line 103:
FI
OD
END</
{{out}}
<pre>
Line 151:
=={{header|AppleScript}}==
===Procedural===
<
-- variant: text containing "Lucas", "oblong", or "prime" — or none of these.
-- n: length of output sequence required.
Line 284:
task()
</syntaxhighlight>
{{output}}
<
0 1 1 3 5 11 21 43 85 171
341 683 1365 2731 5461 10923 21845 43691 87381 174763
Line 303:
First 11 Jacobsthal Primes:
3 5 11 43 683 2731
43691 174763 2796203 715827883 2932031007403"</
===Functional===
<
-- e.g. take(10, jacobsthal())
Line 741:
end |λ|
end script
end zipWith</
{{Out}}
<pre>30 first terms of the Jacobsthal sequence:
Line 771:
=={{header|Arturo}}==
<
JL: function [n]-> (2^n) + (neg 1)^n
JO: function [n]-> (J n) * (J n+1)
Line 798:
printFirst "Jacobsthal-Lucas numbers" 'JL [true] 30
printFirst "Jacobsthal oblong numbers" 'JO [true] 20
printFirst "Jacobsthal primes" 'J [prime? num] 20</
{{out}}
Line 847:
=={{header|AutoHotkey}}==
<
return SubStr(" " Format("{:.0f}", (2**n - (-1)**n ) / 3), -8)
}
Line 881:
ans.push(n)
return ans
}</
Examples:<
loop 30
result .= Jacobsthal(A_Index-1) (mod(A_Index, 5) ? " ":"`n")
Line 901:
MsgBox, 262144, , % result
return</
{{out}}
<pre>First 30 Jacobsthal numbers:
Line 931:
=={{header|C}}==
{{libheader|GMP}}
<
#include <gmp.h>
Line 991:
return 0;
}</
{{out}}
Line 1,042:
=={{header|C++}}==
{{libheader|GMP}}
<
#include <iomanip>
Line 1,089:
}
}
}</
{{out}}
Line 1,140:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
math.primes prettyprint sequences ;
Line 1,165:
"First 20 Jacobsthal primes:" print
20 prime-jacobsthals ltake [ . ] leach</
{{out}}
<pre>
Line 1,214:
=={{header|FreeBASIC}}==
<
If n < 2 Then Return False
If n Mod 2 = 0 Then Return false
Line 1,264:
Swap i0, i1
Loop Until c = 10
Sleep</
{{out}}
<pre>First 30 Jacobsthal numbers:
Line 1,301:
=={{header|Go}}==
<
import (
Line 1,365:
}
}
}</
{{out}}
Line 1,415:
=={{header|Haskell}}==
<
jacobsthal = 0 : 1 : zipWith (\x y -> 2 * x + y) jacobsthal (tail jacobsthal)
Line 1,439:
putStrLn ""
putStrLn "First 10 Jacobsthal primes:"
print $ take 10 $ filter isPrime jacobsthal</
{{out}}
<pre>
Line 1,458:
or, defined in terms of unfoldr:
<
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime)
Line 1,507:
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</
{{Out}}
<pre>30 terms of the Jacobsthal sequence:
Line 1,539:
Implementation:
<
jl=: 2x&^ + _1x&^ NB.Jacobsthal-Lucas</
Task examples:
<
0 1 1 3 5 11 21 43 85 171
341 683 1365 2731 5461 10923 21845 43691 87381 174763
Line 1,556:
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</
=={{header|jq}}==
Line 1,565:
'''Preliminaries'''
<
def chunks(n):
def c: .[0:n], (if length > n then .[n:]|c else empty end);
Line 1,581:
# To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);</
'''The Tasks'''
<
. as $n
| ( (2|power($n)) - (if ($n%2 == 0) then 1 else -1 end)) | divmod(3)[0];
Line 1,609:
;
tasks</
{{out}}
<pre>
Line 1,649:
=={{header|Julia}}==
<
using Primes
Line 1,673:
printrows("Fifteen Jacabsthal prime numbers:", collect(take(15, Jprimes)), 40, 1, false)
</
<pre>
Thirty Jacobsthal numbers:
Line 1,716:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Jacobsthal[n_]:=(2^n-(-1)^n)/3
JacobsthalLucas[n_]:=2^n+(-1)^n
Line 1,734:
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}
Line 1,765:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature <say state>;
Line 1,784:
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;</
{{out}}
<pre>First 30 Jacobsthal numbers:
Line 1,832:
=={{header|Phix}}==
You can run this online [http://phix.x10.mx/p2js/jacobsthal.htm here].
<!--<
<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,865:
<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>
<!--</
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,916:
=={{header|Python}}==
{{trans|Phix}}
<
from math import floor, pow
Line 1,954:
for j in range(3, 33):
if isPrime(jacobsthal(j)):
print(jacobsthal(j))</
{{out}}
Line 1,981:
Or, defining an infinite series in terms of a general unfoldr anamorphism:
<
from itertools import islice
Line 2,149:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>30 terms of the Jacobsthal sequence:
Line 2,178:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my $jacobsthal-lucas = lazy 2, 1, * × 2 + * … *;
Line 2,191:
say "\nFirst 20 Jacobsthal primes:";
say $jacobsthal.grep( &is-prime )[^20].join: "\n";</
{{out}}
<pre>First 30 Jacobsthal numbers:
Line 2,238:
=={{header|Red}}==
<
jacobsthal: function [n] [to-integer (2 ** n - (-1 ** n) / 3)]
Line 2,289:
]
n: n + 1
]</
{{out}}
<pre>
Line 2,328:
=={{header|Rust}}==
<
// rug = "0.3"
Line 2,374:
println!("{}", n);
}
}</
{{out}}
Line 2,424:
=={{header|Sidef}}==
<
lucasU(1, -2, n)
}
Line 2,442:
say "\nFirst 20 Jacobsthal primes:";
say (1..Inf -> lazy.map(jacobsthal).grep{.is_prime}.first(20))</
{{out}}
Line 2,462:
{{trans|go}}
{{incomplete|Vlang|Probably Prime section isn't implemented yet (This is in development)}}
<
fn jacobsthal(n u32) big.Integer {
Line 2,520:
}
}*/
}</
{{out}}
Line 2,552:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
import "./seq" for Lst
import "./fmt" for Fmt
Line 2,584:
}
System.print("\nFirst 20 Jacobsthal primes:")
for (i in 0..19) Fmt.print("$i", primes[i])</
{{out}}
Line 2,634:
=={{header|XPL0}}==
<
int N, I;
[if N <= 2 then return N = 2;
Line 2,687:
J2:= J1; J1:= J;
];
]</
{{out}}
|