Jacobsthal numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add Factor)
(Add Jacobsthal-Lucas numbers)
Line 13: Line 13:
J(n) = ( 2<sup>n</sup> - (-1)<sup>n</sup> ) / 3
J(n) = ( 2<sup>n</sup> - (-1)<sup>n</sup> ) / 3
</span>
</span>


'''Jacobsthal-Lucas number''' are very similar. The only difference is an initial starting value '''J<sub>0</sub> = 2''' rather than '''J<sub>0</sub> = 0'''.




Line 24: Line 27:
;Task
;Task
* Find and display the first 30 '''Jacobsthal numbers'''
* 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 the first 20 '''Jacobsthal oblong numbers'''
* Find and display at least the first 10 '''Jacobsthal primes'''
* Find and display at least the first 10 '''Jacobsthal primes'''
Line 33: Line 37:
;* [https://www.numbersaplenty.com/set/Jacobsthal_number Numbers Aplenty - Jacobsthal number]
;* [https://www.numbersaplenty.com/set/Jacobsthal_number Numbers Aplenty - Jacobsthal number]
;* [[oeis:A001045|OEIS:A001045 - Jacobsthal sequence (or Jacobsthal numbers)]]
;* [[oeis:A001045|OEIS:A001045 - Jacobsthal sequence (or Jacobsthal numbers)]]
;* [[oeis:A014551|OEIS:A014551 - Jacobsthal-Lucas numbers.]]
;* [[oeis:A084175|OEIS:A084175 - Jacobsthal oblong numbers]]
;* [[oeis:A084175|OEIS:A084175 - Jacobsthal oblong numbers]]
;* [[oeis:A049883|OEIS:A049883 - Primes in the Jacobsthal sequence]]
;* [[oeis:A049883|OEIS:A049883 - Primes in the Jacobsthal sequence]]
Line 92: Line 97:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>my $jacobsthal = cache lazy (^∞).hyper.map: { (exp($_, 2) - exp($_, -1)) / 3 };
<lang perl6>my $jacobsthal = cache lazy 0, 1, * × 2 + * *;
my $jacob-lucas = lazy 2, 1, * × 2 + * … *;


say "First 30 Jacobsthal numbers:";
say "First 30 Jacobsthal numbers:";
say $jacobsthal[^30].batch(5)».fmt("%9d").join: "\n";
say $jacobsthal[^30].batch(5)».fmt("%9d").join: "\n";

say "\nFirst 30 Jacobsthal-Lucas numbers:";
say $jacob-lucas[^30].batch(5)».fmt("%9d").join: "\n";


say "\n\nFirst 20 Jacobsthal oblong numbers:";
say "\n\nFirst 20 Jacobsthal oblong numbers:";
Line 110: Line 119:
349525 699051 1398101 2796203 5592405
349525 699051 1398101 2796203 5592405
11184811 22369621 44739243 89478485 178956971
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





Revision as of 18:38, 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 number 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 ;

jacobsthal ( m -- n ) dup 2^ -1 rot ^ - 3 / ;
jacobsthals ( -- list ) 0 lfrom [ jacobsthal ] lmap-lazy ;
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 20 Jacobsthal oblong numbers:" print 20 oblong show

"First 10 Jacobsthal primes:" print 10 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 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 $jacob-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 $jacob-lucas[^30].batch(5)».fmt("%9d").join: "\n";

say "\n\nFirst 20 Jacobsthal oblong numbers:"; say (^∞).map( { $jacobsthal[$_] × $jacobsthal[$_+1] } )[^20].batch(5)».fmt("%11d").join: "\n";

say "\n\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