Jacobsthal numbers
You are encouraged to solve this task according to the task description, using any language you may know.
Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 × Jn-2
Terms may be calculated directly using one of several possible formulas:
Jn = ( 2n - (-1)n ) / 3
Jacobsthal-Lucas numbers are very similar. They have the same recurrence relationship, the only difference is an initial starting value J0 = 2 rather than J0 = 0.
Terms may be calculated directly using one of several possible formulas:
JLn = 2n + (-1)n
Jacobsthal oblong numbers is the sequence obtained from multiplying each Jacobsthal number Jn by its direct successor Jn+1.
Jacobsthal primes are Jacobsthal numbers that are prime.
- Task
- Find and display the first 30 Jacobsthal numbers
- Find and display the first 30 Jacobsthal-Lucas numbers
- Find and display the first 20 Jacobsthal oblong numbers
- Find and display at least the first 10 Jacobsthal primes
- See also
- Wikipedia: Jacobsthal number
- Numbers Aplenty - Jacobsthal number
- OEIS:A001045 - Jacobsthal sequence (or Jacobsthal numbers)
- OEIS:A014551 - Jacobsthal-Lucas numbers.
- OEIS:A084175 - Jacobsthal oblong numbers
- OEIS:A049883 - Primes in the Jacobsthal sequence
- Related task: Fibonacci sequence
- Related task: Leonardo numbers
ALGOL 68
<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 # INT max j prime = 20; # number of Jacobsthal prinmes we will find # PR precision 200 PR # set the precision of LONG LONG INT # PR read "primes.incl.a68" PR # include prime utilities # [ 0 : max jacobsthal ]LONG INT j; # will hold Jacobsthal numbers # [ 0 : max jacobsthal ]LONG INT jl; # will hold Jacobsthal-Lucas numbers # [ 1 : max oblong ]LONG INT jo; # will hold Jacobsthal oblong numbers # # calculate the Jacobsthal Numbers and related numbers # # Jacobsthal : J0 = 0, J1 = 1, Jn = Jn-1 + 2 × Jn-2 # # Jacobsthal-Lucas: JL0 = 2, JL1 = 1, JLn = JLn-1 + 2 × JLn-2 # # Jacobsthal oblong: JOn = Jn x Jn-1 # j[ 0 ] := 0; j[ 1 ] := 1; jl[ 0 ] := 2; jl[ 1 ] := 1; jo[ 1 ] := 0; FOR n FROM 2 TO UPB j DO j[ n ] := j[ n - 1 ] + ( 2 * j[ n - 2 ] ); jl[ n ] := jl[ n - 1 ] + ( 2 * jl[ n - 2 ] ) OD; FOR n TO UPB jo DO jo[ n ] := j[ n ] * j[ n - 1 ] OD; # prints an array of numbers with the specified legend # PROC show numbers = ( STRING legend, []LONG INT numbers )VOID: BEGIN INT n count := 0; print( ( "First ", whole( ( UPB numbers - LWB numbers ) + 1, 0 ), " ", legend, newline ) ); FOR n FROM LWB numbers TO UPB numbers DO print( ( " ", whole( numbers[ n ], -11 ) ) ); IF ( n count +:= 1 ) MOD 5 = 0 THEN print( ( newline ) ) FI OD END # show numbers # ; # show the various numbers numbers # show numbers( "Jacobsthal Numbers:", j ); show numbers( "Jacobsthal-Lucas Numbers:", jl ); show numbers( "Jacobsthal oblong Numbers:", jo ); # find some prime Jacobsthal numbers # LONG LONG INT jn1 := j[ 1 ], jn2 := j[ 0 ]; INT p count := 0; print( ( "First ", whole( max j prime, 0 ), " Jacobstal primes:", newline ) ); print( ( " n Jn", newline ) ); FOR n FROM 2 WHILE p count < max j prime DO LONG LONG INT jn = jn1 + ( 2 * jn2 ); jn2 := jn1; jn1 := jn; IF is probably prime( jn ) THEN # have a probably prime Jacobsthal number # p count +:= 1; print( ( whole( n, -4 ), ": ", whole( jn, 0 ), newline ) ) FI OD
END</lang>
- Output:
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 Jacobstal primes: n Jn 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
AppleScript
<lang applescript>on jacobsthalNumbers(variant, n)
-- variant: text containing "Lucas", "oblong", or "prime" — or none of these. -- n: length of output sequence required. -- The two Jacobsthal numbers preceding the current 'j'. Initially the first two in the sequence. set {anteprev, prev} to {0, 1} -- Default plug-in script. Its handler simply appends the current 'j' to the output. script o property output : {anteprev, prev} on append(dummy, j) set end of output to j end append end script -- If a variant sequence is specified, change the first value or substitute -- a script whose handler decides the values to append to the output. ignoring case if (variant contains "Lucas") then set anteprev to 2 set o's output's first item to anteprev else if (variant contains "oblong") then script property output : {0} on append(prev, j) set end of output to prev * j end append end script set o to result else if (variant contains "prime") then script property output : {} on append(dummy, j) if (isPrime(j)) then set end of output to j end append end script set o to result end if end ignoring -- Work through the Jacobsthal process until the required output length is obtained. repeat until ((count o's output) = n) set j to anteprev + anteprev + prev tell o to append(prev, j) set anteprev to prev set prev to j end repeat return o's output
end jacobsthalNumbers
on isPrime(n)
if (n < 3) then return (n is 2) if (n mod 2 is 0) then return false repeat with i from 3 to (n ^ 0.5) div 1 by 2 if (n mod i is 0) then return false end repeat return true
end isPrime
-- Task and presentation of results!: on intToText(n)
set txt to "" repeat until (n < 100000000) set txt to text 2 thru 9 of (100000000 + (n mod 100000000) div 1 as text) & txt set n to n div 100000000 end repeat return (n as integer as text) & txt
end intToText
on chopList(theList, sublistLen)
script o property lst : theList property output : {} end script set listLen to (count o's lst) repeat with i from 1 to listLen by sublistLen set j to i + sublistLen - 1 if (j > listLen) then set j to listLen set end of o's output to items i thru j of o's lst end repeat return o's output
end chopList
on matrixToText(matrix, w)
script o property matrix : missing value property row : missing value end script set o's matrix to matrix set padding to " " repeat with r from 1 to (count o's matrix) set o's row to o's matrix's item r repeat with i from 1 to (count o's row) set o's row's item i to text -w thru end of (padding & o's row's item i) end repeat set o's matrix's item r to join(o's row, "") end repeat return join(o's matrix, linefeed)
end matrixToText
on join(lst, delim)
set astid to AppleScript's text item delimiters set AppleScript's text item delimiters to delim set txt to lst as text set AppleScript's text item delimiters to astid return txt
end join
on task()
set output to {"First 30 Jacobsthal Numbers:", "First 30 Jacobsthal-Lucas Numbers:", ¬ "First 20 Jacobsthal oblong Numbers:", "First 11 Jacobsthal Primes:"} set results to {jacobsthalNumbers("", 30), jacobsthalNumbers("Lucas", 30), ¬ jacobsthalNumbers("oblong", 20), jacobsthalNumbers("prime", 11)} repeat with i from 1 to 4 set thisSequence to item i of results repeat with j in thisSequence set j's contents to intToText(j) end repeat if (i < 4) then set theLines to chopList(thisSequence, 10) else set theLines to chopList(thisSequence, 6) end if set item i of output to item i of output & linefeed & matrixToText(theLines, (count end of thisSequence) + 1) end repeat return join(output, linefeed & linefeed)
end task
task() </lang>
- Output:
<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 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 11 Jacobsthal Primes:
3 5 11 43 683 2731 43691 174763 2796203 715827883 2932031007403"</lang>
C
<lang c>#include <stdio.h>
- include <gmp.h>
void jacobsthal(mpz_t r, unsigned long n) {
mpz_t s; mpz_init(s); mpz_set_ui(r, 1); mpz_mul_2exp(r, r, n); mpz_set_ui(s, 1); if (n % 2) mpz_neg(s, s); mpz_sub(r, r, s); mpz_div_ui(r, r, 3);
}
void jacobsthal_lucas(mpz_t r, unsigned long n) {
mpz_t a; mpz_init(a); mpz_set_ui(r, 1); mpz_mul_2exp(r, r, n); mpz_set_ui(a, 1); if (n % 2) mpz_neg(a, a); mpz_add(r, r, a);
}
int main() {
int i, count; mpz_t jac[30], j; printf("First 30 Jacobsthal numbers:\n"); for (i = 0; i < 30; ++i) { mpz_init(jac[i]); jacobsthal(jac[i], i); gmp_printf("%9Zd ", jac[i]); if (!((i+1)%5)) printf("\n"); }
printf("\nFirst 30 Jacobsthal-Lucas numbers:\n"); mpz_init(j); for (i = 0; i < 30; ++i) { jacobsthal_lucas(j, i); gmp_printf("%9Zd ", j); if (!((i+1)%5)) printf("\n"); }
printf("\nFirst 20 Jacobsthal oblong numbers:\n"); for (i = 0; i < 20; ++i) { mpz_mul(j, jac[i], jac[i+1]); gmp_printf("%11Zd ", j); if (!((i+1)%5)) printf("\n"); }
printf("\nFirst 20 Jacobsthal primes:\n"); for (i = 0, count = 0; count < 20; ++i) { jacobsthal(j, i); if (mpz_probab_prime_p(j, 15) > 0) { gmp_printf("%Zd\n", j); ++count; } }
return 0;
}</lang>
- Output:
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
C++
<lang cpp>#include <gmpxx.h>
- include <iomanip>
- include <iostream>
using big_int = mpz_class;
bool is_probably_prime(const big_int& n) {
return mpz_probab_prime_p(n.get_mpz_t(), 30) != 0;
}
big_int jacobsthal_number(unsigned int n) {
return ((big_int(1) << n) - (n % 2 == 0 ? 1 : -1)) / 3;
}
big_int jacobsthal_lucas_number(unsigned int n) {
return (big_int(1) << n) + (n % 2 == 0 ? 1 : -1);
}
big_int jacobsthal_oblong_number(unsigned int n) {
return jacobsthal_number(n) * jacobsthal_number(n + 1);
}
int main() {
std::cout << "First 30 Jacobsthal Numbers:\n"; for (unsigned int n = 0; n < 30; ++n) { std::cout << std::setw(9) << jacobsthal_number(n) << ((n + 1) % 5 == 0 ? '\n' : ' '); } std::cout << "\nFirst 30 Jacobsthal-Lucas Numbers:\n"; for (unsigned int n = 0; n < 30; ++n) { std::cout << std::setw(9) << jacobsthal_lucas_number(n) << ((n + 1) % 5 == 0 ? '\n' : ' '); } std::cout << "\nFirst 20 Jacobsthal oblong Numbers:\n"; for (unsigned int n = 0; n < 20; ++n) { std::cout << std::setw(11) << jacobsthal_oblong_number(n) << ((n + 1) % 5 == 0 ? '\n' : ' '); } std::cout << "\nFirst 20 Jacobsthal primes:\n"; for (unsigned int n = 0, count = 0; count < 20; ++n) { auto jn = jacobsthal_number(n); if (is_probably_prime(jn)) { ++count; std::cout << jn << '\n'; } }
}</lang>
- Output:
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
Factor
<lang factor>USING: grouping io kernel lists lists.lazy math math.functions math.primes prettyprint sequences ;
- 2^-1^ ( n -- 2^n -1^n ) dup 2^ -1 rot ^ ;
- jacobsthal ( m -- n ) 2^-1^ - 3 / ;
- jacobsthal-lucas ( m -- n ) 2^-1^ + ;
- as-list ( quot -- list ) 0 lfrom swap lmap-lazy ; inline
- jacobsthals ( -- list ) [ jacobsthal ] as-list ;
- lucas-jacobthals ( -- list ) [ jacobsthal-lucas ] as-list ;
- prime-jacobsthals ( -- list ) jacobsthals [ prime? ] lfilter ;
- show ( n list -- ) ltake list>array 5 group simple-table. nl ;
- oblong ( -- list )
jacobsthals dup cdr lzip [ product ] lmap-lazy ;
"First 30 Jacobsthal numbers:" print 30 jacobsthals show
"First 30 Jacobsthal-Lucas numbers:" print 30 lucas-jacobthals show
"First 20 Jacobsthal oblong numbers:" print 20 oblong show
"First 20 Jacobsthal primes:" print 20 prime-jacobsthals ltake [ . ] leach</lang>
- Output:
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
FreeBASIC
<lang freebasic>Function isPrime(n As Ulongint) As Boolean
If n < 2 Then Return False If n Mod 2 = 0 Then Return false For i As Uinteger = 3 To Int(Sqr(n))+1 Step 2 If n Mod i = 0 Then Return false Next i Return true
End Function
Dim Shared As Uinteger n(1) Dim Shared As Uinteger i0 = 0, i1 = 1 Dim Shared As Integer j, c, P = 1, Q = -2
Print "First 30 Jacobsthal numbers:" c = 0 : n(i0) = 0: n(i1) = 1 For j = 0 To 29
c += 1 Print Using " #########"; n(i0); Print Iif (c Mod 5, "", !"\n"); n(i0) = P * n(i1) - Q * n(i0) Swap i0, i1
Next j
Print !"\n\nFirst 30 Jacobsthal-Lucas numbers: " c = 0 : n(i0) = 2: n(i1) = 1 For j = 0 To 29
c += 1 Print Using " #########"; n(i0); Print Iif (c Mod 5, "", !"\n"); n(i0) = P * n(i1) - Q * n(i0) Swap i0, i1
Next j
Print !"\n\nFirst 20 Jacobsthal oblong numbers: " c = 0 : n(i0) = 0: n(i1) = 1 For j = 0 To 19
c += 1 Print Using " ###########"; n(i0)*n(i1); Print Iif (c Mod 5, "", !"\n"); n(i0) = P * n(i1) - Q * n(i0) Swap i0, i1
Next j
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) n(i0) = P * n(i1) - Q * n(i0) Swap i0, i1
Loop Until c = 10 Sleep</lang>
- Output:
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
Go
<lang go>package main
import (
"fmt" "math/big"
)
func jacobsthal(n uint) *big.Int {
t := big.NewInt(1) t.Lsh(t, n) s := big.NewInt(1) if n%2 != 0 { s.Neg(s) } t.Sub(t, s) return t.Div(t, big.NewInt(3))
}
func jacobsthalLucas(n uint) *big.Int {
t := big.NewInt(1) t.Lsh(t, n) a := big.NewInt(1) if n%2 != 0 { a.Neg(a) } return t.Add(t, a)
}
func main() {
jac := make([]*big.Int, 30) fmt.Println("First 30 Jacobsthal numbers:") for i := uint(0); i < 30; i++ { jac[i] = jacobsthal(i) fmt.Printf("%9d ", jac[i]) if (i+1)%5 == 0 { fmt.Println() } }
fmt.Println("\nFirst 30 Jacobsthal-Lucas numbers:") for i := uint(0); i < 30; i++ { fmt.Printf("%9d ", jacobsthalLucas(i)) if (i+1)%5 == 0 { fmt.Println() } }
fmt.Println("\nFirst 20 Jacobsthal oblong numbers:") for i := uint(0); i < 20; i++ { t := big.NewInt(0) fmt.Printf("%11d ", t.Mul(jac[i], jac[i+1])) if (i+1)%5 == 0 { fmt.Println() } }
fmt.Println("\nFirst 20 Jacobsthal primes:") for n, count := uint(0), 0; count < 20; n++ { j := jacobsthal(n) if j.ProbablyPrime(10) { fmt.Println(j) count++ } }
}</lang>
- Output:
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
Haskell
<lang haskell>jacobsthal :: [Integer] jacobsthal = 0 : 1 : zipWith (\x y -> 2 * x + y) jacobsthal (tail jacobsthal)
jacobsthalLucas :: [Integer] jacobsthalLucas = 2 : 1 : zipWith (\x y -> 2 * x + y) jacobsthalLucas (tail jacobsthalLucas)
jacobsthalOblong :: [Integer] jacobsthalOblong = zipWith (*) jacobsthal (tail jacobsthal)
isPrime :: Integer -> Bool isPrime n = n > 1 && not (or [n `mod` i == 0 | i <- [2 .. floor (sqrt (fromInteger n))]])
main :: IO () main = do
putStrLn "First 30 Jacobsthal numbers:" print $ take 30 jacobsthal putStrLn "" putStrLn "First 30 Jacobsthal-Lucas numbers:" print $ take 30 jacobsthalLucas putStrLn "" putStrLn "First 20 Jacobsthal oblong numbers:" print $ take 20 jacobsthalOblong putStrLn "" putStrLn "First 10 Jacobsthal primes:" print $ take 10 $ filter isPrime jacobsthal</lang>
- Output:
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]
or, defined in terms of unfoldr:
<lang haskell>import Data.List (intercalate, transpose, uncons, unfoldr) import Data.List.Split (chunksOf) import Data.Numbers.Primes (isPrime) import Text.Printf (printf)
JACOBSTHAL NUMBERS ------------------
jacobsthal :: [Integer] jacobsthal = jacobsthalish [0, 1]
jacobsthalish :: [Integer] -> [Integer] jacobsthalish = unfoldr go
where go [] = Nothing go [_] = Nothing go (a : b : xs) = Just (a, b : xs <> [2 * a + b])
TEST -------------------------
main :: IO () main =
mapM_ (putStrLn . format) [ ( "terms of the Jacobsthal sequence", 30, jacobsthal ), ( "Jacobsthal-Lucas numbers", 30, jacobsthalish [2, 1] ), ( "Jacobsthal oblong numbers", 20, zipWith (*) jacobsthal (tail jacobsthal) ), ( "Jacobsthal primes", 10, filter isPrime jacobsthal ) ]
format :: (String, Int, [Integer]) -> String format (k, n, xs) =
show n <> (' ' : k) <> ":\n" <> table " " (chunksOf 5 $ show <$> take n xs)
table :: String -> String -> String table gap rows =
let ws = maximum . fmap length <$> transpose rows pw = printf . flip intercalate ["%", "s"] . show in unlines $ intercalate gap . zipWith pw ws <$> rows</lang>
- Output:
30 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 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 Jacobsthal oblong numbers: 0 1 3 15 55 231 903 3655 14535 58311 232903 932295 3727815 14913991 59650503 238612935 954429895 3817763271 15270965703 61084037575 10 Jacobsthal primes: 3 5 11 43 683 2731 43691 174763 2796203 715827883
J
Implementation:
<lang J>ja=: 3 %~ 2x&^ - _1x&^ NB. Jacobsthal jl=: 2x&^ + _1x&^ NB.Jacobsthal-Lucas</lang>
Task examples:
<lang 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
349525 699051 1398101 2796203 5592405 11184811 22369621 44739243 89478485 178956971
jl i.3 10 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 10$2 */\ ja i.21 NB. Jacobsthal oblong 0 1 3 15 55 231 903 3655 14535 58311
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</lang>
jq
Works with gojq, the Go implementation of jq
See Erdős-primes#jq for a suitable definition of `is_prime` as used here. As a practical matter, this function limits the exploration of Jacobsthal primes.
Preliminaries <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); c;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
- If $j is 0, then an error condition is raised;
- otherwise, assuming infinite-precision integer arithmetic,
- if the input and $j are integers, then the result will be a pair of integers.
def divmod($j):
. as $i | ($i % $j) as $mod | [($i - $mod) / $j, $mod] ;
- To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);</lang>
The Tasks <lang jq>def jacobsthal:
. as $n | ( (2|power($n)) - (if ($n%2 == 0) then 1 else -1 end)) | divmod(3)[0];
def jacobsthalLucas:
. as $n | (2|power($n)) + (if ($n%2 == 0) then 1 else -1 end);
def tasks:
def pp($width): chunks(5) | map(lpad($width)) | join(""); [range(0;30) | jacobsthal] as $js | "First 30 Jacobsthal numbers:", ( $js | pp(12)),
"\nFirst 30 Jacobsthal-Lucas numbers:", ( [range(0;30) | jacobsthalLucas] | pp(12)), "\nFirst 20 Jacobsthal oblong numbers:", ( [range(0;20) | $js[.] * $js[1+.]] | pp(14)),
"\nFirst 11 Jacobsthal primes:", limit(11; range(0; infinite) | jacobsthal | select(is_prime))
tasks</lang>
- Output:
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 11 Jacobsthal primes: 3 5 11 43 683 2731 43691 174763 2796203 715827883 2932031007403
Julia
<lang julia>using Lazy using Primes
J(n) = (2^n - (-1)^n) ÷ 3 L(n) = 2^n + (-1)^n
Jacobsthal = @>> Lazy.range(0) map(J) JLucas = @>> Lazy.range(0) map(L) Joblong = @>> Lazy.range(big"0") map(n -> J(n) * J(n + 1)) Jprimes = @>> Lazy.range(big"0") map(J) filter(isprime)
function printrows(title, vec, columnsize = 15, columns = 5, rjust=true)
println(title) for (i, n) in enumerate(vec) print((rjust ? lpad : rpad)(n, columnsize), i % columns == 0 ? "\n" : "") end println()
end
printrows("Thirty Jacobsthal numbers:", collect(take(30, Jacobsthal))) printrows("Thirty Jacobsthal-Lucas numbers:", collect(take(30, JLucas))) printrows("Twenty oblong Jacobsthal numbers:", collect(take(20, Joblong))) printrows("Fifteen Jacabsthal prime numbers:", collect(take(15, Jprimes)), 40, 1, false)
</lang>
- Output:
Thirty 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 Thirty 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 Twenty oblong Jacobsthal numbers: 0 1 3 15 55 231 903 3655 14535 58311 232903 932295 3727815 14913991 59650503 238612935 954429895 3817763271 15270965703 61084037575 Fifteen Jacabsthal prime numbers: 3 5 11 43 683 2731 43691 174763 2796203 715827883 2932031007403 768614336404564651 201487636602438195784363 845100400152152934331135470251 56713727820156410577229101238628035243
Perl
<lang perl>use strict; use warnings; use feature <say state>; use bigint; use List::Util 'max'; use ntheory 'is_prime';
sub table { my $t = 5 * (my $c = 1 + length max @_); ( sprintf( ('%'.$c.'d')x@_, @_) ) =~ s/.{1,$t}\K/\n/gr }
sub jacobsthal { my($n) = @_; state @J = (0, 1); do { push @J, $J[-1] + 2 * $J[-2]} until @J > $n; $J[$n] } sub jacobsthal_lucas { my($n) = @_; state @JL = (2, 1); do { push @JL, $JL[-1] + 2 * $JL[-2]} until @JL > $n; $JL[$n] }
my(@j,@jp,$c,$n); push @j, jacobsthal $_ for 0..29; do { is_prime($n = ( 2**++$c - -1**$c ) / 3) and push @jp, $n } until @jp == 20;
say "First 30 Jacobsthal numbers:\n", table @j; 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;</lang>
- Output:
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
Phix
You can run this online here.
with javascript_semantics function jacobsthal(integer n) return floor((power(2,n)+odd(n))/3) end function function jacobsthal_lucas(integer n) return power(2,n)+power(-1,n) end function function jacobsthal_oblong(integer n) return jacobsthal(n)*jacobsthal(n+1) end function printf(1,"First 30 Jacobsthal numbers:\n%s\n", {join_by(apply(true,sprintf,{{"%9d" },apply(tagset(29,0),jacobsthal)}),1,5," ")}) printf(1,"First 30 Jacobsthal-Lucas numbers:\n%s\n", {join_by(apply(true,sprintf,{{"%9d" },apply(tagset(29,0),jacobsthal_lucas)}),1,5," ")}) printf(1,"First 20 Jacobsthal oblong numbers:\n%s\n",{join_by(apply(true,sprintf,{{"%11d"},apply(tagset(19,0),jacobsthal_oblong)}),1,5," ")}) --printf(1,"First 10 Jacobsthal primes:\n%s\n", {join(apply(true,sprintf,{{"%d"},filter(apply(tagset(31,0),jacobsthal),is_prime)}),"\n")}) --hmm(""), fine, but to go further roll out gmp: include mpfr.e mpz z = mpz_init() integer n = 1, found = 0 printf(1,"First 20 jacobsthal primes:\n") while found<20 do mpz_ui_pow_ui(z,2,n) mpz_add_ui(z,z,odd(n)) {} = mpz_fdiv_q_ui(z,z,3) if mpz_prime(z) then found += 1 printf(1,"%s\n",{mpz_get_str(z)}) end if n += 1 end while
Likewise should you want the three basic functions to go further they'll have to look much more like the C submission above.
- Output:
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
Python
<lang python>#!/usr/bin/python from math import floor, pow
def isPrime(n):
for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True
def odd(n):
return n and 1 != 0
def jacobsthal(n):
return floor((pow(2,n)+odd(n))/3)
def jacobsthal_lucas(n):
return int(pow(2,n)+pow(-1,n))
def jacobsthal_oblong(n):
return jacobsthal(n)*jacobsthal(n+1)
if __name__ == '__main__':
print("First 30 Jacobsthal numbers:") for j in range(0, 30): print(jacobsthal(j), end=" ")
print("\n\nFirst 30 Jacobsthal-Lucas numbers: ") for j in range(0, 30): print(jacobsthal_lucas(j), end = '\t')
print("\n\nFirst 20 Jacobsthal oblong numbers: ") for j in range(0, 20): print(jacobsthal_oblong(j), end=" ")
print("\n\nFirst 10 Jacobsthal primes: ") for j in range(3, 33): if isPrime(jacobsthal(j)): print(jacobsthal(j))</lang>
- Output:
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
Or, defining infinite series in terms of a general unfoldr anamorphism:
<lang python>Jacobsthal numbers
from itertools import islice from operator import mul
- jacobsthal :: [Integer]
def jacobsthal():
Infinite sequence of terms of OEIS A001045 return jacobsthalish((0, 1))
- jacobsthalish :: (Int, Int) -> [Int]
def jacobsthalish(ab):
Infinite sequence of jacobsthal-type series beginning with a, b def go(xs): a, *t = xs return a, t + [2 * a + t[0]]
return unfoldr(go)(list(ab))
- ------------------------- TEST -------------------------
- main :: IO ()
def main():
First 15 terms each n-step Fibonacci(n) series where n is drawn from [2..8] print('\n\n'.join([ fShow(*x) for x in [ ( 'terms of the Jacobsthal sequence', 30, jacobsthal()), ( 'Jacobsthal-Lucas numbers', 30, jacobsthalish((2, 1)) ), ( 'Jacobsthal oblong numbers', 20, map( mul, jacobsthal(), drop(1)(jacobsthal()) ) ), ( 'primes in the Jacobsthal sequence', 10, filter(isPrime, jacobsthal()) ) ] ]))
- fShow :: (String, Int, [Integer]) -> String
def fShow(k, n, xs):
N tabulated terms of XS, prefixed by the label K return f'{n} {k}:\n' + spacedTable( list(chunksOf(5)( [str(t) for t in take(n)(xs)] )) )
- ----------------------- GENERIC ------------------------
- drop :: Int -> [a] -> [a]
- drop :: Int -> String -> String
def drop(n):
The sublist of xs beginning at (zero-based) index n. def go(xs): if isinstance(xs, (list, tuple, str)): return xs[n:] else: take(n)(xs) return xs return go
- isPrime :: Int -> Bool
def isPrime(n):
True if n is prime. if n in (2, 3): return True if 2 > n or 0 == n % 2: return False if 9 > n: return True if 0 == n % 3: return False
def p(x): return 0 == n % x or 0 == n % (2 + x)
return not any(map(p, range(5, 1 + int(n ** 0.5), 6)))
- take :: Int -> [a] -> [a]
- take :: Int -> String -> String
def take(n):
The prefix of xs of length n, or xs itself if n > length xs. def go(xs): return ( xs[0:n] if isinstance(xs, (list, tuple)) else list(islice(xs, n)) ) return go
- unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
def unfoldr(f):
Generic anamorphism. A lazy (generator) list unfolded from a seed value by repeated application of f until no residue remains. Dual to fold/reduce. f returns either None, or just (value, residue). For a strict output value, wrap in list(). def go(x): valueResidue = f(x) while None is not valueResidue: yield valueResidue[0] valueResidue = f(valueResidue[1]) return go
- ---------------------- FORMATTING ----------------------
- chunksOf :: Int -> [a] -> a
def chunksOf(n):
A series of lists of length n, subdividing the contents of xs. Where the length of xs is not evenly divisible, the final list will be shorter than n. def go(xs): return ( xs[i:n + i] for i in range(0, len(xs), n) ) if 0 < n else None return go
- spacedTable :: String -> String
def spacedTable(rows):
Tabulated stringification of rows columnWidths = [ max([len(x) for x in col]) for col in zip(*rows) ] return '\n'.join([ ' '.join( map( lambda x, w: x.rjust(w, ' '), row, columnWidths ) ) for row in rows ])
- MAIN ---
if __name__ == '__main__':
main()</lang>
- Output:
30 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 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 Jacobsthal oblong numbers: 0 1 3 15 55 231 903 3655 14535 58311 232903 932295 3727815 14913991 59650503 238612935 954429895 3817763271 15270965703 61084037575 10 primes in the Jacobsthal sequence: 3 5 11 43 683 2731 43691 174763 2796203 715827883
Raku
<lang perl6>my $jacobsthal = cache lazy 0, 1, * × 2 + * … *; my $jacobsthal-lucas = lazy 2, 1, * × 2 + * … *;
say "First 30 Jacobsthal numbers:"; say $jacobsthal[^30].batch(5)».fmt("%9d").join: "\n";
say "\nFirst 30 Jacobsthal-Lucas numbers:"; say $jacobsthal-lucas[^30].batch(5)».fmt("%9d").join: "\n";
say "\nFirst 20 Jacobsthal oblong numbers:"; say (^∞).map( { $jacobsthal[$_] × $jacobsthal[$_+1] } )[^20].batch(5)».fmt("%11d").join: "\n";
say "\nFirst 20 Jacobsthal primes:"; say $jacobsthal.grep( &is-prime )[^20].join: "\n";</lang>
- Output:
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
Red
<lang rebol>Red ["Jacobsthal numbers"]
jacobsthal: function [n] [to-integer (2 ** n - (-1 ** n) / 3)]
lucas: function [n] [2 ** n + (-1 ** n)]
oblong: function [n] [
first split mold multiply to-float jacobsthal n to-float jacobsthal n + 1 #"." ; work around integer overflow
]
prime?: function [
"Returns true if the input is a prime number" n [number!] "An integer to check for primality"
][
if 2 = n [return true] if any [1 = n even? n] [return false] limit: sqrt n candidate: 3 while [candidate < limit][ if n % candidate = 0 [return false] candidate: candidate + 2 ] true
]
show: function [n fn][
cols: length? mold fn n repeat i n [ prin [pad fn subtract i 1 cols] if i % 5 = 0 [prin newline] ] prin newline
]
print "First 30 Jacobsthal numbers:" show 30 :jacobsthal
print "First 30 Jacobsthal-Lucas numbers:" show 30 :lucas
print "First 20 Jacobsthal oblong numbers:" show 20 :oblong
print "First 10 Jacobsthal primes:" primes: n: 0 while [primes < 10][
if prime? jacobsthal n [ print jacobsthal n primes: primes + 1 ] n: n + 1
]</lang>
- Output:
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
Rust
<lang rust>// [dependencies] // rug = "0.3"
use rug::integer::IsPrime; use rug::Integer;
fn jacobsthal_numbers() -> impl std::iter::Iterator<Item = Integer> {
(0..).map(|x| ((Integer::from(1) << x) - if x % 2 == 0 { 1 } else { -1 }) / 3)
}
fn jacobsthal_lucas_numbers() -> impl std::iter::Iterator<Item = Integer> {
(0..).map(|x| (Integer::from(1) << x) + if x % 2 == 0 { 1 } else { -1 })
}
fn jacobsthal_oblong_numbers() -> impl std::iter::Iterator<Item = Integer> {
let mut jn = jacobsthal_numbers(); let mut n0 = jn.next().unwrap(); std::iter::from_fn(move || { let n1 = jn.next().unwrap(); let result = Integer::from(&n0 * &n1); n0 = n1; Some(result) })
}
fn jacobsthal_primes() -> impl std::iter::Iterator<Item = Integer> {
jacobsthal_numbers().filter(|x| x.is_probably_prime(30) != IsPrime::No)
}
fn main() {
println!("First 30 Jacobsthal Numbers:"); for (i, n) in jacobsthal_numbers().take(30).enumerate() { print!("{:9}{}", n, if (i + 1) % 5 == 0 { "\n" } else { " " }); } println!("\nFirst 30 Jacobsthal-Lucas Numbers:"); for (i, n) in jacobsthal_lucas_numbers().take(30).enumerate() { print!("{:9}{}", n, if (i + 1) % 5 == 0 { "\n" } else { " " }); } println!("\nFirst 20 Jacobsthal oblong Numbers:"); for (i, n) in jacobsthal_oblong_numbers().take(20).enumerate() { print!("{:11}{}", n, if (i + 1) % 5 == 0 { "\n" } else { " " }); } println!("\nFirst 20 Jacobsthal primes:"); for n in jacobsthal_primes().take(20) { println!("{}", n); }
}</lang>
- Output:
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
Sidef
<lang ruby>func jacobsthal(n) {
lucasU(1, -2, n)
}
func lucas_jacobsthal(n) {
lucasV(1, -2, n)
}
say "First 30 Jacobsthal numbers:" say 30.of(jacobsthal)
say "\nFirst 30 Jacobsthal-Lucas numbers:" say 30.of(lucas_jacobsthal)
say "\nFirst 20 Jacobsthal oblong numbers:" say 21.of(jacobsthal).cons(2, {|a,b| a * b })
say "\nFirst 20 Jacobsthal primes:"; say (1..Inf -> lazy.map(jacobsthal).grep{.is_prime}.first(20))</lang>
- Output:
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]
Wren
<lang ecmascript>import "./big" for BigInt import "./seq" for Lst import "./fmt" for Fmt
var jacobsthal = Fn.new { |n| ((BigInt.one << n) - ((n%2 == 0) ? 1 : -1)) / 3 }
var jacobsthalLucas = Fn.new { |n| (BigInt.one << n) + ((n%2 == 0) ? 1 : -1) }
System.print("First 30 Jacobsthal numbers:") var js = (0..29).map { |i| jacobsthal.call(i) }.toList for (chunk in Lst.chunks(js, 5)) Fmt.print("$,12i", chunk)
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.print("$,12i", chunk)
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.print("$,14i", chunk)
var primes = js.where { |j| j.isProbablePrime(10) }.toList var count = primes.count var i = 31 while (count < 20) {
var j = jacobsthal.call(i) if (j.isProbablePrime(10)) { primes.add(j) count = count + 1 } i = i + 1
} System.print("\nFirst 20 Jacobsthal primes:") for (i in 0..19) Fmt.print("$i", primes[i])</lang>
- Output:
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 First 20 Jacobsthal primes: 3 5 11 43 683 2731 43691 174763 2796203 715827883 2932031007403 768614336404564651 201487636602438195784363 845100400152152934331135470251 56713727820156410577229101238628035243 62357403192785191176690552862561408838653121833643 1046183622564446793972631570534611069350392574077339085483 267823007376498379256993682056860433753700498963798805883563 5562466239377370006237035693149875298444543026970449921737087520370363869220418099018130434731 95562442332919646317117537304253622533190207882011713489066201641121786503686867002917439712921903606443
XPL0
<lang XPL0>func IsPrime(N); \Return 'true' if N is prime int N, I; [if N <= 2 then return N = 2; if (N&1) = 0 then \even >2\ return false; for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false; I:= I+1; ];
return true; ];
proc Jaco(J2); \Display 30 Jacobsthal (or -Lucas) numbers real J2, J1, J; int N; [RlOut(0, J2); J1:= 1.0; RlOut(0, J1); for N:= 2 to 30-1 do
[J:= J1 + 2.0*J2; RlOut(0, J); if rem((N+1)/5) = 0 then CrLf(0); J2:= J1; J1:= J; ];
CrLf(0); ];
real J, J1, J2, JO; int N; [Format(14, 0); Jaco(0.0); Jaco(2.0); J2:= 1.0; RlOut(0, 0.0); J1:= 1.0; RlOut(0, J1); for N:= 2 to 20-1 do
[J:= (J1 + 2.0*J2); JO:= J*J1; RlOut(0, JO); if rem((N+1)/5) = 0 then CrLf(0); J2:= J1; J1:= J; ];
CrLf(0); J2:= 0.0; J1:= 1.0; N:= 0; loop [J:= J1 + 2.0*J2;
if IsPrime(fix(J)) then [RlOut(0, J); N:= N+1; if rem(N/5) = 0 then CrLf(0); if N >= 10 then quit; ]; J2:= J1; J1:= J; ];
]</lang>
- Output:
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