PHP and Simple math problem

Jecob Boman

Well-known member
  • Dec 10, 2013
    1,333
    93
    48
    homagama
    මෙක ගණිතයේ තියෙන රටා හදුනාගැනීම වගේ දෙයක්. php සම්බන්ද දෙයක්.

    මේකයි array එකක් තියෙනව auto generate වෙන. එන්නේ මේ රිසල්ට් එක.
    1, 3, 5 කියල. ඔයාලට පේනව ඇති නේ රටාවක්. එකට දෙකක් එකතු උනාම 3යි. 3ට දෙකක් එකතු උනාම 5යි.

    මම මේ රටාව ටිකක් වෙනස් කරා. හැම array element එකකින්ම 1ක් අඩු කරල. දැන් එන්නේ

    0, 2, 4 කියල. ඒත් මට මේක වෙනස් කරගන්න ඕන. 0, 1, 2 ආදී වශයෙන් එන ආකාරයකට.

    මෙන්න මේ php statement එකේ තමයි ඔය දේ වෙන්නේ.


    PHP:
    $title = str_repeat('— ', $depth - [COLOR="Red"]1[/COLOR]) . $iterator->current();

    මේකට ගොඩක් මැත්ස් තමයි ඕන කරන්නේ. පුලුවන්ද කාට හරි සපෝට් එකක් දෙන්න. :)
     
    Last edited:

    imhotep

    Well-known member
  • Mar 29, 2017
    14,823
    8
    35,327
    113
    Are you trying to generate Fibonacci series....?
    This will list the first 25....

    <?php

    function Fibonacci($n){

    $no1 = 0;
    $no2 = 1;

    $counter = 0;
    while ($counter < $n){
    echo ' '.$no1;
    $no3 = $no2 + $no1;
    $no1 = $no2;
    $no2 = $no3;
    $counter = $counter + 1;
    }
    }

    $n = 25;
    Fibonacci($n);
     

    Jecob Boman

    Well-known member
  • Dec 10, 2013
    1,333
    93
    48
    homagama
    Are you trying to generate Fibonacci series....?
    This will list the first 25....

    <?php

    function Fibonacci($n){

    $no1 = 0;
    $no2 = 1;

    $counter = 0;
    while ($counter < $n){
    echo ' '.$no1;
    $no3 = $no2 + $no1;
    $no1 = $no2;
    $no2 = $no3;
    $counter = $counter + 1;
    }
    }

    $n = 25;
    Fibonacci($n);

    thanks machan. :)