Jacobsthal numbers: Difference between revisions

m
syntax highlighting fixup automation
(Added AutoHotkey)
m (syntax highlighting fixup automation)
Line 53:
{{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:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 151:
=={{header|AppleScript}}==
===Procedural===
<langsyntaxhighlight lang="applescript">on jacobsthalNumbers(variant, n)
-- variant: text containing "Lucas", "oblong", or "prime" — or none of these.
-- n: length of output sequence required.
Line 284:
 
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 303:
First 11 Jacobsthal Primes:
3 5 11 43 683 2731
43691 174763 2796203 715827883 2932031007403"</langsyntaxhighlight>
 
===Functional===
<langsyntaxhighlight lang="applescript">-------------------- JACOBSTHAL NUMBERS ------------------
 
-- e.g. take(10, jacobsthal())
Line 741:
end |λ|
end script
end zipWith</langsyntaxhighlight>
{{Out}}
<pre>30 first terms of the Jacobsthal sequence:
Line 771:
=={{header|Arturo}}==
 
<langsyntaxhighlight 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)
Line 798:
printFirst "Jacobsthal-Lucas numbers" 'JL [true] 30
printFirst "Jacobsthal oblong numbers" 'JO [true] 20
printFirst "Jacobsthal primes" 'J [prime? num] 20</langsyntaxhighlight>
 
{{out}}
Line 847:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Jacobsthal(n){
return SubStr(" " Format("{:.0f}", (2**n - (-1)**n ) / 3), -8)
}
Line 881:
ans.push(n)
return ans
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">result := "First 30 Jacobsthal numbers:`n"
loop 30
result .= Jacobsthal(A_Index-1) (mod(A_Index, 5) ? " ":"`n")
Line 901:
 
MsgBox, 262144, , % result
return</langsyntaxhighlight>
{{out}}
<pre>First 30 Jacobsthal numbers:
Line 931:
=={{header|C}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <gmp.h>
 
Line 991:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,042:
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <gmpxx.h>
 
#include <iomanip>
Line 1,089:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,140:
=={{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 1,165:
 
"First 20 Jacobsthal primes:" print
20 prime-jacobsthals ltake [ . ] leach</langsyntaxhighlight>
{{out}}
<pre>
Line 1,214:
 
=={{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 1,264:
Swap i0, i1
Loop Until c = 10
Sleep</langsyntaxhighlight>
{{out}}
<pre>First 30 Jacobsthal numbers:
Line 1,301:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,365:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,415:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">jacobsthal :: [Integer]
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</langsyntaxhighlight>
{{out}}
<pre>
Line 1,458:
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 1,507:
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 1,539:
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 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</langsyntaxhighlight>
 
=={{header|jq}}==
Line 1,565:
 
'''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 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);</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 1,609:
;
 
tasks</langsyntaxhighlight>
{{out}}
<pre>
Line 1,649:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Lazy
using Primes
 
Line 1,673:
printrows("Fifteen Jacabsthal prime numbers:", collect(take(15, Jprimes)), 40, 1, false)
 
</langsyntaxhighlight>{{out}}
<pre>
Thirty Jacobsthal numbers:
Line 1,716:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Jacobsthal, JacobsthalLucas, JacobsthalOblong]
Jacobsthal[n_]:=(2^n-(-1)^n)/3
JacobsthalLucas[n_]:=2^n+(-1)^n
Line 1,734:
i++;
]][[2,1]]//Grid
</syntaxhighlight>
</lang>
{{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}}
<langsyntaxhighlight lang="perl">use strict;
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;</langsyntaxhighlight>
{{out}}
<pre>First 30 Jacobsthal numbers:
Line 1,832:
=={{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,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>
<!--</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,916:
=={{header|Python}}==
{{trans|Phix}}
<langsyntaxhighlight lang="python">#!/usr/bin/python
from math import floor, pow
 
Line 1,954:
for j in range(3, 33):
if isPrime(jacobsthal(j)):
print(jacobsthal(j))</langsyntaxhighlight>
 
{{out}}
Line 1,981:
Or, defining an infinite series in terms of a general unfoldr anamorphism:
 
<langsyntaxhighlight lang="python">'''Jacobsthal numbers'''
 
from itertools import islice
Line 2,149:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>30 terms of the Jacobsthal sequence:
Line 2,178:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my $jacobsthal = cache lazy 0, 1, * × 2 + * … *;
my $jacobsthal-lucas = lazy 2, 1, * × 2 + * … *;
 
Line 2,191:
 
say "\nFirst 20 Jacobsthal primes:";
say $jacobsthal.grep( &is-prime )[^20].join: "\n";</langsyntaxhighlight>
{{out}}
<pre>First 30 Jacobsthal numbers:
Line 2,238:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red ["Jacobsthal numbers"]
 
jacobsthal: function [n] [to-integer (2 ** n - (-1 ** n) / 3)]
Line 2,289:
]
n: n + 1
]</langsyntaxhighlight>
{{out}}
<pre>
Line 2,328:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// rug = "0.3"
 
Line 2,374:
println!("{}", n);
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,424:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func jacobsthal(n) {
lucasU(1, -2, n)
}
Line 2,442:
 
say "\nFirst 20 Jacobsthal primes:";
say (1..Inf -> lazy.map(jacobsthal).grep{.is_prime}.first(20))</langsyntaxhighlight>
 
{{out}}
Line 2,462:
{{trans|go}}
{{incomplete|Vlang|Probably Prime section isn't implemented yet (This is in development)}}
<langsyntaxhighlight lang="vlang">import math.big
 
fn jacobsthal(n u32) big.Integer {
Line 2,520:
}
}*/
}</langsyntaxhighlight>
 
{{out}}
Line 2,552:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "./big" for BigInt
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])</langsyntaxhighlight>
 
{{out}}
Line 2,634:
 
=={{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 2,687:
J2:= J1; J1:= J;
];
]</langsyntaxhighlight>
 
{{out}}
10,327

edits