Categories
Games History PHP

Fibonacci Series in PHP

What is Fibonacci Series?

Fibonacci series is the sequence where the first two numbers are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two e.g. 0, 1, 1, 2, 3, 5, 8 etc.

The Fibonacci sequence is named after Italian mathematician Fibonacci, although the sequence had been described earlier earlier as Virahanka numbers in Indian mathematics.
Fibonacci numbers are closely related to Lucas numbers in that they are a complementary pair of Lucas sequences.

Fibonacci

The Fibonacci numbers are important in the computational run-time analysis of Euclid’s algorithm to determine the greatest common divisor of two integers: the worst case input for this algorithm is a pair of consecutive Fibonacci numbers.

Recognizing Fibonacci numbers

The question may arise whether a positive integer x is a Fibonacci number or not. This is true if and only if one or both of 5x^{2}+4 or 5x^{2}-4 is a perfect square.

Fibonacci Series without recursion:

<?php
$term = 10;
$num1 = 0;
$num2 = 1;

for ( $i = 0; $i < $term; $i++ ) {
    if ( $i <= 1 ) { 
        $result = $i;
    } else {
        $result = $num1 + $num2;
        $num1 = $num2;
        $num2 = $result;
    }
    echo ' ' . $result;
}
?>

Fibonacci Series using recursion:

<?php
function fibonacci($n)
{
    if ( $n == 0 ) {
        return 0;
    } else if ( $n == 1 ) {
        return 1;
    } else {
        return ( fibonacci( $n - 1 ) + fibonacci( $n - 2 ) );
    }
}

$term = 10;
for ( $i = 0; $i &amp;amp;lt; $term; $i++ ) {
    echo ' '.fibonacci($i);
}
?>

Output: 0 1 1 2 3 5 8 13 21 34

Fun fact

Fibonacci Day is November 23rd, as it has the digits “1, 1, 2, 3” which is part of the sequence. So next Nov 23 let everyone know!

'Coz sharing is caring
Categories
PHP

Palindrome in php

What is Palindrome?

A palindrome is a word, phrase, number, or sequence of characters which reads the same in either direction. The words A and I are the simplest and smallest palindromes. Few of the most commonly used are :-
RACECAR LEVEL CIVIC POP MADAM EYE NUN RADAR

The word palindrome is derived from the Greek palíndromos, meaning running back again (palín = AGAIN + drom–, drameîn = RUN).

Words like LIVE and STRAW (which read EVIL and WARTS backwards) are not themselves palindromes but the phrases LIVE EVIL and STRAW WARTS are. A palindrome is not necessarily a single word.

While creating palindrome, it is usually accepted that punctuation and word spacings are ignored, and so the famous MADAM, I’M ADAM is a valid palindrome.

Here are a few good ones:

    Do geese see God?
    Murder for a jar of red rum.
    Some men interpret nine memos.
    Never odd or even.

palindrome
A famous palindrome “Able was I ere I saw Elba” purportedly spoken by Napoleon, referring to his first sighting of Elba, the island where the British exiled him.

How to verify a word is palindrome ?

Find given string is palindrome or not in PHP using native strrev() function

<?php
$word = 'madam';  // declare a varibale
echo 'String: <b>' . $word . '</b>';
$reverse = strrev($word); // reverse the word
if ($word == $reverse) // compare if the original word is same as the reverse of it
    echo ' is a palindrome';
else
    echo ' is not a palindrome';
?>

Find given string is palindrome or not in PHP without using strrev() function

<?php
$mystring = 'madam'; // set the string
echo 'String: <b>' . $mystring . '</b>';

$myArray = array(); // initialize an array
$myArray = str_split($mystring); //split into array
$len = sizeof($myArray); // get the size of array
$newString = '';

for ( $i = $len; $i >= 0; $i-- ) {
    $newString .= $myArray[$i];
}

if ( $mystring == $newString ) {
    echo ' is a palindrome';
} else {
    echo ' is not a palindrome';
}
?>
'Coz sharing is caring