public function resultado() { if ($this->fib == '') { "Não foi digitado um Numero"; } else { require_once 'fibonacci.php'; $f = new Fibonacci(); $data['fibonacci'] = $f->fibonacci($this->fib); require_once 'view/resultado.php'; } }
/** * @param integer $n * * @return integer */ public static function get($n) { if (isset(self::$cache[$n])) { return self::$cache[$n]; } self::validate($n); foreach (Fibonacci::sequence($n + 1) as $key => $number) { self::$cache[$key] = $number; } return self::$cache[$n]; }
/** * Calculate the Fibonacci number of the given integer. * @param int n If n <= 0 then 0 is assumed. * @return int Fibonacci number. */ public static function fib($n) { if ($n <= 0) { return 0; } else { if ($n == 1) { return 1; } else { return Fibonacci::fib($n - 1) + Fibonacci::fib($n - 2); } } }
<?php class Fibonacci { private $f = array(0, 1, 1); public function get($n) { if (!isset($this->f[$n])) { $this->f[$n] = $this->get($n - 1) + $this->get($n - 2); } return $this->f[$n]; } } $fibonacciNumbers = new Fibonacci(); foreach (file($argv[1]) as $line) { echo $fibonacciNumbers->get(trim($line)) . "\n"; }
<?php // inclui no documento a pagina que contem a Classe Fibonacci require_once 'fibonacci.php'; // Instancia um novo Objeto da Classe Fibonacci $fibonacci = new Fibonacci(); // verifica se chego o numero para que //possa ser chamado o metodo "fibonacci(numero)" da Classe Fibonacci $numero = isset($_POST['numero']) ? $_POST['numero'] : ''; // mostra na tela o resultado do metodo "fibonacci(numero)" da Classe Fibonacci echo $fibonacci->fibonacci($numero);
/** * @expectedException InvalidArgumentException * @expectedExceptionMessage Application only accepts integers. Input was: string */ public function testInvalidArgumentNotInteger() { Fibonacci::get("string"); }
<?php require_once 'fibonacci.php'; $fibonacci = new Fibonacci(); // N1 recebe um numero pela URL $numero = isset($_GET['numero']) ? $_GET['numero'] : ''; echo $fibonacci->fibonacci($this->fib);
} public function key() { return $this->key; } public function next() { if ($this->value === 0) { $this->value = 1; } else { $old = $this->value; $this->value += $this->sum; $this->sum = $old; } $this->key++; } public function valid() { return $this->value < PHP_INT_MAX; } } // print the Fibonacci numbers until PHP_INT_MAX foreach ($test = new Fibonacci() as $key => $value) { printf("%d) %d\n", $key, $value); } // print the first 10 Fibonacci's numbers $num = new Fibonacci(); for ($i = 0; $i < 10; $i++) { printf("%d) %d\n", $i, $num->current()); $num->next(); }
* Copyright 2015 The University of Edinburgh. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. */ namespace Maths; require "Fibonacci.php"; /** * Print usage information. */ function printUsage() { print "Usage: php FibonacciProgram.php N\n"; print " where N >= 0\n"; } if (count($argv) < 2) { printUsage(); } else { print Fibonacci::fib($argv[1]); print "\n"; }
$this->num_termos = $n; } /** * Retorna o enésimo termo da sequência de Fibonacci */ public function termo($n) { if ($n > $this->num_termos) { $this->calcula($n); } return $this->termos[$n - 1]; } /** * Retorna a soma dos enésimos termos da sequência de Fibonacci */ public function soma($n) { return $this->termo($n + 2) - 1; } /** * Imprime os termos calculados */ private function debug() { for ($i = 0; $i < $this->num_termos; $i++) { echo "Posição {$i} valor " . $this->termos[$i] . "<br>"; } } } $F = new Fibonacci(); echo "Soma dos 50 primeiros termos da sequência de Fibonacci -> <a href=\"https://sexlog.com/prova_" . $F->soma(50) . "\">" . $F->soma(50) . "</a>";
/** Test Fibonacci::fib(30). */ public function testFib30() { $this->assertEquals(832040, Fibonacci::fib(30)); }
public function current() { return $this->value; } public function key() { return $this->key; } public function next() { if ($this->value === 0) { $this->value = 1; } else { $old = $this->value; $this->value += $this->sum; $this->sum = $old; } $this->key++; } public function valid() { return true; } } // check the first 10 Fibonacci's numbers $num = new Fibonacci(); $correct = array(0, 1, 1, 2, 3, 5, 8, 13, 21, 34); for ($i = 0; $i < 10; $i++) { printf("%s<br>", $num->current() === $correct[$i] ? 'OK ' : 'ERROR'); $num->next(); }
<body> <div class="cabecalho"> <span id="imagem-cabecalho-1"></span> <span id="titulo">Teste da classe Fibonacci</span> <span id="imagem-cabecalho-1"></span> </div> <a href="http://pt.wikipedia.org/wiki/N%C3%BAmero_de_Fibonacci">Saiba mais sobre a constante</a> <div class="corpo"> <?php //incluíndo o arquivo da classe. include_once 'classes/fibonacci.class.php'; //Instanciando um objeto. $objetoFibonacci = new Fibonacci(); //echo $objetoFibonacci->fibonacciRecursive(5); //Usa a função que mostra N termos (um número grande - maior do que 20- acarreta lentidão) //Um valor como 30, por exemplo, pode levar mais de 60 segundos. echo $objetoFibonacci->calculaNTermos(5); //Apresenta erro pois o valor para calcular um termo tem que ser positivo. echo $objetoFibonacci->fibonacciRecursive(-2); //Verificando os tempos de início e fim do cálculo echo 'Tempo incial: ' . $objetoFibonacci->getInicialTimeExecution() . '<br/>'; echo 'Tempo final: ' . $objetoFibonacci->getFinalTimeExecution(); ?> </div> <div class="footer"> <div class="html-validator"> <p>
<?php /** * Created by PhpStorm. * User: bamdad * Date: 2/3/2016 * Time: 9:02 PM */ include_once './autoload.php'; $fibo = new Fibonacci(); $fibo->sequence(10);
function generator() { if ($this->n == 0) { echo "exit if"; print_r($this->array); return 0; } $this->a = $this->b + $this->c; $this->b = $this->c; $this->c = $this->a; $this->array[$this->counter] = $this->b; if ($this->b != 0) { $this->counter++; } $this->n = $this->n - 1; return $this->generator(); } } $fibonacci = new Fibonacci(5, 9); echo $fibonacci->generator(); // function fib($a,$n) { // $c = 0; // $b = $a; // for($i = 0; $i < $n; $i++) { // $a = $b + $c; // $b = $c; // $c = $a; // echo $i . " number in fib sequence = " . $b . "\n"; // } // } // fib(5,9);