Leonardo numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
(Sijkstra -> Dijkstra)
Line 35: Line 35:




[[wp:Edsger W. Dijkstra|Edsger W. Sijkstra]]   used them as an integral part of
[[wp:Edsger W. Dijkstra|Edsger W. Dijkstra]]   used them as an integral part of
his   [[wp:smoothsort|smoothsort]]   [[wp:algorithm|algorithm]].
his   [[wp:smoothsort|smoothsort]]   [[wp:algorithm|algorithm]].



Revision as of 10:46, 20 May 2017

Leonardo 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.


The   Leonardo numbers   are a sequence of numbers defined by:

   L(0) = 1
   L(1) = 1
   L(n) = L(n-1)  +    L(n-2)   +  1
       also
   L(n) =      2  *  Fib(n+1)   -  1

  where the   + 1   (herein)   will be known as the   add   number.
  where the   FIB   is the   Fibonacci numberss.


The task will be using the 3rd equation (above) to calculate the Leonardo numbers.


Edsger W. Dijkstra   used them as an integral part of his   smoothsort   algorithm.


The first few Leonardo numbers are:

    1   1   3   5   9   15   25   41   67   109   177   287   465   753   1219   1973   3193   5167   8361  ··· 


Task
  •   show the 1st   25   Leonardo numbers, starting at L(0).
  •   allow the first two Leonardo numbers to be specified   [for L(0) and L(1)].
  •   allow the   add   number to be specified   (1 is the default).
  •   show the 1st   25   Leonardo numbers, specifying 0 and 1 for L(0) and L(1), and 0 for the add number.

(The last task requirement will produce the Fibonacci numbers.)


Show all output here.


Related tasks


See also



Perl 6

<lang perl6>my $N = 25;

  1. Part 1

my @𝑳 = 1, 1, { $^n2 + $^n1 + 1 } ... *; say "The first $N Leonardo numbers are:"; put @𝑳[^$N];

  1. Part 2

my $𝑳0 = 0; my $𝑳1 = 1; my $𝑳add = 0; say "\nThe first $N numbers using 𝑳0 of $𝑳0, 𝑳1 of $𝑳1, and adder of $𝑳add:"; @𝑳 = $𝑳0, $𝑳1, { $^n2 + $^n1 + $𝑳add } ... *; put @𝑳[^$N];</lang>

Output:
The first 25 Leonardo numbers are:
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049

The first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368

REXX

<lang rexx>/*REXX pgm computes Leonardo numbers, allowing the specification of L(0), L(1), and ADD#*/ numeric digits 500 /*just in case the user gets ka-razy. */ @.=1 /*define the default for the @. array.*/ parse arg N L0 L1 a# . /*obtain optional arguments from the CL*/ if N == | N =="," then N= 25 /*Not specified? Then use the default.*/ if L0\== & L0\=="," then @.0= L0 /*Was " " " " value. */ if L1\== & L1\=="," then @.1= L1 /* " " " " " " */ if a#\== & a#\=="," then @.a= a# /* " " " " " " */ say 'The first ' N " Leonardo numbers are:" /*display a title for the output series*/ if @.0\==1 | @.1\==1 then say 'using ' @.0 " for L(0)" if @.0\==1 | @.1\==1 then say 'using ' @.1 " for L(1)" if @.a\==1 then say 'using ' @.a " for the add number" say /*display blank line before the output.*/ $= /*initialize the output line to "null".*/

            do j=0  for N                       /*construct a list of Leonardo numbers.*/
            if j<2  then z=@.j                  /*for the 1st two numbers, use the fiat*/
                    else do                     /*··· otherwise, compute the Leonardo #*/
                         _=@.0                  /*save the old primary Leonardo number.*/
                         @.0=@.1                /*store the new primary number in old. */
                         @.1=@.0  +  _  +  @.a  /*compute the next Leonardo number.    */
                         z=@.1                  /*store the next Leonardo number in Z. */
                         end                    /* [↑]  only 2 Leonardo #s are stored. */
            $=$ z                               /*append the just computed # to $ list.*/
            end   /*j*/                         /* [↓]  elide the leading blank in  $. */

say strip($) /*stick a fork in it, we're all done. */</lang>

output   when using the default input:
The first  25  Leonardo numbers are:

1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
output   when using the input of:     12   0   1   0
The first  25  Leonardo numbers are:
using  0  for L(0)
using  1  for L(1)
using  0  for the  add  number

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368