Jacobsthal numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Julia}}: edit conflict. two people trying to mark incorrect at the same time. merged reports)
Line 145: Line 145:


=={{header|Julia}}==
=={{header|Julia}}==
{{incorrect|Jacobsthal oblong definition should be Joblong = @>> Lazy.range(big"0") map(n -> J(n) * J(n + 1))}}
{{incorrect|Julia|Jacobsthals start at 0. Jacobsthal-Lucas isn't just Jacobsthal shifted left. Jacobsthal oblongs missing every other term. Primes seem to be correct though. 👍 <br>Jacobsthal oblong definition should be <nowiki>Joblong = @>> Lazy.range(big"0") map(n -> J(n) * J(n + 1))</nowiki>}}
<lang julia>using Lazy
<lang julia>using Lazy
using Primes
using Primes

Revision as of 22:12, 26 February 2022

Jacobsthal numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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 directly calculated using the formula:


   J(n) = ( 2n - (-1)n ) / 3


Jacobsthal-Lucas numbers are very similar. The only difference is an initial starting value J0 = 2 rather than J0 = 0.


Jacobsthal oblong numbers is the sequence obtained from multiplying each two consecutive Jacobsthal numbers together.


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



Factor

Works with: Factor version 0.99 2021-06-02

<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

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>

Julia

This example is incorrect. Please fix the code and remove this message.

Details: Jacobsthals start at 0. Jacobsthal-Lucas isn't just Jacobsthal shifted left. Jacobsthal oblongs missing every other term. Primes seem to be correct though. 👍
Jacobsthal oblong definition should be Joblong = @>> Lazy.range(big"0") map(n -> J(n) * J(n + 1))

<lang julia>using Lazy using Primes

J(n) = (2^n - (-1)^n) ÷ 3

Jacobsthal = @>> Lazy.range() map(J) JLucas = @>> Lazy.range(2) map(J) Joblong = @>> Lazy.range(big"0") map(n -> J(2n) * J(2n + 1)) Jprimes = @>> Lazy.range(big"0") map(J) filter(isprime)

lazyprintnum(s) = println(replace("$s", r"[^\d\,\s]" => ""))

lazyprintnum(collect(take(30, Jacobsthal))) lazyprintnum(collect(take(30, JLucas))) lazyprintnum(collect(take(20, Joblong))) lazyprintnum(collect(take(15, Jprimes)))

</lang>

Output:
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, 357913941
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, 357913941, 715827883
0, 3, 55, 903, 14535, 232903, 3727815, 59650503, 954429895, 15270965703, 244335800775, 3909374210503, 62549992960455, 1000799909736903, 16012798645268935, 256204778682216903, 4099276460347126215, 65588423371280642503, 1049414773963396772295, 16790636383505974325703
3, 5, 11, 43, 683, 2731, 43691, 174763, 2796203, 715827883, 2932031007403, 768614336404564651, 201487636602438195784363, 845100400152152934331135470251, 56713727820156410577229101238628035243

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

atom t0 = time()
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")})
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

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

Wren

Library: Wren-big
Library: Wren-seq
Library: Wren-fmt

<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