예제 #1
1
function fibonacci($number)
{
    if ($number <= 2) {
        return 1;
    }
    return fibonacci($number - 1) + fibonacci($number - 2);
}
function fibonacci($n)
{
    if ($n <= 1) {
        return $n;
    } else {
        return fibonacci($n - 1) + fibonacci($n - 2);
    }
}
    $splitNumber = str_split($i);
    foreach ($splitNumber as $seperatedNumber) {
        if ($seperatedNumber == 4) {
            $n++;
            break;
        }
    }
}
echo $n . PHP_EOL;
// ------------------------------------------> 2nd challenge
function fibonacci($n)
{
    $squareOfNumber = sqrt(5 * $n * $n - 4);
    $secondSquareOfNumber = sqrt(5 * $n * $n + 4);
    if ($squareOfNumber - ceil($squareOfNumber) == 0) {
        echo "{$n} is fibonacci" . PHP_EOL;
    } else {
        if ($secondSquareOfNumber - ceil($secondSquareOfNumber) == 0) {
            echo "{$n} is fibonacci" . PHP_EOL;
        } else {
            echo "{$n} is not a fibonacci" . PHP_EOL;
        }
    }
}
fibonacci(89);
fibonacci(233);
fibonacci(375);
fibonacci(601);
fibonacci(1597);
fibonacci(4183);
예제 #4
0
function fibonacci($n)
{
    if ($n === 1 || $n === 2) {
        return 1;
    }
    return fibonacci($n - 1) + fibonacci($n - 2);
}
예제 #5
0
 public function run()
 {
     $r = 0;
     for ($n = 1; $n <= 5; $n++) {
         $r += fibonacci($n);
     }
 }
예제 #6
0
/**
 * Sequência de Fibonacci
 *
 * Autor:
 *   ?
 * Colaborador:
 *   Felipe Djinn <*****@*****.**>
 * Tipo:
 *   math
 * Descrição:
 *   Na matemática, os Números de Fibonacci são uma sequência definida como recursiva.
 *   O algoritmo recursivo que define a série aplica-se, na prática, conforme a regra sugere: 
 *   começa-se a série com 0 e 1; a seguir, obtém-se o próximo número de Fibonacci somando-se 
 *   os dois anteriores e, assim, sucessiva e infinitamente.
 * Complexidade:
 *   F(n) = {
 *      0 se n = 0;
 *      1 se n = 1;
 *      F(n - 1) + F(n - 2)
 *   }
 * Referências:
 *   http://pt.wikipedia.org/wiki/N%C3%BAmero_de_Fibonacci
 */
function fibonacci($n)
{
    if ($n < 2) {
        return $n;
    }
    return fibonacci($n - 1) + fibonacci($n - 2);
}
function fibonacci($n)
{
    if ($n == 0 || $n == 1) {
        return $n;
    }
    return fibonacci($n - 2) + fibonacci($n - 1);
}
function fibonacci($n)
{
    if ($n < 4) {
        return 1;
    }
    //in a fib3 the sum of the first three numbers constitutes the sum
    return fibonacci($n - 1) + fibonacci($n - 2) + fibonacci($n - 3);
}
 public function testPositiveNumberFibonnacci()
 {
     // Given the next order
     // Keys     [0..14]
     // Values   [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377]
     $this->assertEquals(fibonacci(14), 377);
     $this->assertEquals(fibonacci_iterative(14), 377);
 }
예제 #10
0
파일: fibonacci.php 프로젝트: cosim/fuzuli
function fibonacci($x)
{
    if ($x <= 2) {
        return 1;
    } else {
        return fibonacci($x - 1) + fibonacci($x - 2);
    }
}
예제 #11
0
function fibonacci($n)
{
    if ($n < 3) {
        return 1;
    } else {
        return fibonacci($n - 1) + fibonacci($n - 2);
    }
}
예제 #12
0
function fibonacci($n)
{
    if ($n < 2) {
        return $n . "<br>";
    } else {
        return fibonacci($n - 1) + fibonacci($n - 2);
    }
}
예제 #13
0
function fibonacci($end)
{
    $number = null;
    if ($end <= 1) {
        echo $end;
    } else {
        $number = fibonacci($end - 2) + fibonacci($end - 1);
    }
    echo $number . ' ';
}
예제 #14
0
파일: train.php 프로젝트: elzup/CodeIQ
function fibonacci($n)
{
    if ($n == 0) {
        return 0;
    }
    if ($n <= 2) {
        return 1;
    }
    return fibonacci($n - 1) + fibonacci($n - 2);
}
예제 #15
0
function fibonacci($term_1, $term_2)
{
    $sum = $term_1 + $term_2;
    if ($sum < 4000000) {
        if ($sum % 2 == 0) {
            $GLOBALS['sum'] += $sum;
        }
        fibonacci($term_2, $sum);
    }
}
예제 #16
0
파일: fibonacci2.php 프로젝트: beeher/hw
function fibonacci($num)
{
    if ($num == 0) {
        return 0;
    } else {
        if ($num == 1) {
            return 1;
        } else {
            return fibonacci($num - 1) + fibonacci($num - 2);
        }
    }
}
function fibonacci($num)
{
    $a = $num - 1;
    $b = $num - 2;
    if ($num >= 2) {
        return fibonacci($a) + fibonacci($b);
    } elseif ($num == 1) {
        return 1;
    } else {
        return 0;
    }
}
예제 #18
0
function fibonacci($n)
{
    global $memo;
    if (!isset($memo[$n])) {
        if ($n == 1 || $n == 2) {
            $memo[$n] = 1;
        } else {
            $memo[$n] = fibonacci($n - 1) + fibonacci($n - 2);
        }
    }
    return $memo[$n];
}
예제 #19
0
파일: fibonacci.php 프로젝트: adhocore/dsa
/**
 * fibonacci. 
 * 
 * @link   https://en.wikipedia.org/wiki/Fibonacci_number.
 *
 * @param  int $n The position of series
 *
 * @return int    The $nth number in fibonacci series
 *
 * @throws InvalidArgumentException  If input is not valid number
 */
function fibonacci($n)
{
    if (!is_numeric($n)) {
        throw new \InvalidArgumentException(sprintf('%s is not valid number', $n));
    }
    if ($n < 0) {
        return $n % 2 ? fibonacci(-$n) : 0 - fibonacci(-$n);
    } elseif ($n == 0) {
        return 0;
    } elseif ($n == 1 or $n == 2) {
        return 1;
    }
    return fibonacci($n - 1) + fibonacci($n - 2);
}
예제 #20
0
function fibonacci($n)
{
    if ($n == 0) {
        return 0;
    }
    if ($n == -1 || $n == 1) {
        return 1;
    } else {
        if ($n > 0) {
            return fibonacci($n - 1) + fibonacci($n - 2);
        } else {
            return fibonacci($n + 2) - fibonacci($n + 1);
        }
    }
}
예제 #21
0
function fibonacci($n)
{
    static $memoFibo;
    $memoFibo[0] = 0;
    $memoFibo[1] = 1;
    $result = 0;
    //if (array_key_exists($n, $memoFibo))
    if (isset($memoFibo[$n])) {
        $result = $memoFibo[$n];
    } else {
        $result = fibonacci($n - 1) + fibonacci($n - 2);
        $memoFibo[$n] = $result;
    }
    return $result;
}
예제 #22
0
function fibonacci($nth)
{
    global $cached;
    if ($nth < 3) {
        return 1;
    }
    if ($cached) {
        if (isset($cached[$nth])) {
            return $cached[$nth];
        }
        if (isset($cached[$nth - 1]) && isset($cached[$nth - 2])) {
            return bcadd($cached[$nth - 1], $cached[$nth - 2]);
        }
    }
    return bcadd(fibonacci($nth - 1), fibonacci($nth - 2));
}
예제 #23
0
function fibonacci($n)
{
    if ($n < 0) {
        return -1;
    }
    if ($n == 0) {
        return 0;
    }
    if ($n == 1) {
        return 1;
    } else {
        if ($n > 1) {
            return fibonacci($n - 1) + fibonacci($n - 2);
        }
    }
}
예제 #24
0
 /**
  * Main program.
  *
  * @param array $args Command-line arguments.
  * @return integer Zero on success; non-zero on failure.
  */
 public static function main($args)
 {
     $status = 0;
     printf("3\n");
     printf("n\n");
     printf("fib1 s\n");
     printf("fib2 s\n");
     for ($i = 0; $i < 48; ++$i) {
         $timer = new Timer();
         $timer->start();
         $result = fibonacci($i);
         $timer->stop();
         $time1 = $timer->getElapsedTime();
         $timer->start();
         $result = fibonacci2($i);
         $timer->stop();
         $time2 = $timer->getElapsedTime();
         printf("%d\t%f\t%f\n", $i, $time1, $time2);
     }
     return $status;
 }
예제 #25
0
파일: index.php 프로젝트: anatai/manray
<?php

error_reporting(E_ALL);
ini_set("display_errors", "On");
require_once 'functions.php';
$square = squared_numbers(15);
$fibo = fibonacci(100);
$name = name('greetings', 'gertrude');
$lotto = lottery();
var_dump($lotto);
예제 #26
0
/**
 * 通过数组方式得到fibonacci的第n项
 * @param  [type] $num [description]
 * @return [type]      [description]
 */
function arrayFeature($num)
{
    $arr = array(1, 1);
    if ($num <= 2) {
        return 1;
    }
    for ($i = 2; $i < $num; $i++) {
        $arr[$i] = $arr[$i - 1] + $arr[$i - 2];
    }
    return $arr[$num - 1];
}
function fibonacci($num, $handle)
{
    $result = array();
    if (gettype($handle) != "function") {
        // return;
    }
    for ($i = 1; $i <= $num; $i++) {
        $result[] = $handle($i);
    }
    return $result;
}
$res = fibonacci(4, 'recursive');
$res = fibonacci(4, 'arrayFeature');
$res = fibonacci(4, 'looping');
echo json_encode($res);
예제 #27
0
파일: index.php 프로젝트: kaboomstinks/test
<?php

function sortArray($arr)
{
    rsort($arr);
    return $arr[1];
}
//$arr = [10,1,3,18,13];
//echo sortArray($arr);
function fibonacci($num)
{
    $arr = [1, 1];
    for ($i = 1; $i < $num; $i++) {
        $arr[] = $arr[$i - 1] + $arr[$i];
    }
    return $arr[$num - 1];
}
echo fibonacci(6);
//echo '<br /><br />';
echo 'Hello World';
예제 #28
0
<!--Zadanie *. Napisz funkcję, która oblicza kolejne wyrazy ciągu Fibonacciego
(http://pl.wikipedia.org/wiki/Ci%C4%85g_Fibonacciego) aż do podanej zmiennej. Nie
używaj do tego rekurencji.-->
<!DOCTYPE html>
<html lang="pl-PL">
<head>
    <meta charset="utf-8">
</head>
<?php 
function fibonacci($n)
{
    $tab[0] = 0;
    $tab[1] = 1;
    for ($i = 0; $i < $n; $i++) {
        $tab[$i + 2] = $tab[$i] + $tab[$i + 1];
        echo $i + 1 . " element ciągu to: " . $tab[$i] . "<br>";
    }
    return $tab[$i + 2];
}
$n = 10;
//ile wyrazów ciągu ma obliczyć
fibonacci($n);
예제 #29
0
use CrystalPlanet\Redshift\Channel\Channel;
function fibonacci($c, $quit)
{
    $x = 0;
    $y = 1;
    while (true) {
        list($value, $channel) = (yield Channel::any([$c, $x], $quit));
        switch ($channel) {
            case $c:
                $tmp = $x + $y;
                $x = $y;
                $y = $tmp;
                break;
            case $quit:
                echo $value;
                return;
        }
    }
}
Redshift::run(function () {
    $c = new Channel();
    $quit = new Channel();
    async(function ($c, $quit) {
        for ($i = 0; $i < 10; ++$i) {
            $n = (yield $c->read());
            echo "{$n}\n";
        }
        (yield $quit->write("Quit\n"));
    }, $c, $quit);
    (yield fibonacci($c, $quit));
});
/**
 * Fibonnaci implementation on a recursive way, using the
 * ternary operation to apply the condition
 *
 * @param   int     $n  The number of the secuence 0 to n
 */
function fibonacci($n = 0)
{
    return $n < 2 ? $n : fibonacci($n - 1) + fibonacci($n - 2);
}