PHP Question

Hi,
I have these 2 question, which seems very difficult for me as i am new. I am seeking answer for these. The answer need to be in function form

Q1. Write a function that prints the Nth number in the Fibonacci sequences to standard output

Sample Input
 
N: 4
 
Sample Output
 
2
 
Additional Samples
 
Input              output
N: 10               14
N:15                377    
N:21                6765

function fibbocanni($n){
   //code answer goes here
}

and
Palindromes are strings, which are identical when reserved.
Write a function, which prints to standard out “T” if a string is a palindrome, and “F” in any other case

Sample Input
 
Words: {“radar”, “apple”, “hello”,”evitative”}
 
Sample Output
TFTT
Additional Samples
 
Input                                                              Output
Words: {“noon”,”orange”,”wow”}                      TFT

function Palindromes($word){

}

I’m sure you’ve already thought about the solution, right?
Write down in plain english how you would solve the problem, then translate those steps in code.
And if you’re stuck with a certain piece of code, just post it here and explain what your problem is.

Challenge accepted!
BUT I will only give you the stupid solution for the first one :slight_smile:
Why stupid? Because it’s with recursion and, for number 15 (as example) the result is 610 but it passes 114243 times through the function.
For no. 22 it took over 5 min and I got bored and stopped it. So, very non-efficient.

I will think at a nice solution in future.
But maybe, someone else comes with one before me :slight_smile:


<?php

error_reporting(E_ALL);
set_time_limit(0);

if( false == isset($_GET['n']) ) {
	die('set n=...');
}
$passed = 0;
echo
	fibo($_GET['n']),
	'<br />Passed: ',
	$passed,
	' times';

function fibo( $n ) {
	global $passed;
	$passed++;
	if( $n == 0 ) {
		return 0;
	}
	if( $n <= 2 ) {
		return 1;
	}
	for( $i = 3; $i <= $n; $i++ ) {
		// var_dump($counter);
		$number = fibo( $i-1 ) + fibo( $i-2 );
	}
	return $number;
}

The palindrome one should be easier.


<?php

function Palindromes($word) {
	$reverse = strrev($word);
	if($reverse == $word){
	 echo "T";
	}else{
	 echo "F";
	}
}

?>

I m confused over your answer, can you put it in a function like

function fibbocanniCOLOR=#009900[/COLOR]{
//code answer goes here
}

He did. It was called fibo

The code doesn’t give any output.

Access it using name_of_script.php?n=4 and you’ll see output. If you read the file, you’d see he was using $_GET to receive the number in the sequence to output.

Well, for the first one you need to define what the fibonacci (not the fibocanni) sequence is. Yes, i know what it is. No, i’m not doing your work for you. There are plenty of optimized ways to find the Nth fibonacci sequence number (in fact, you can do it as a N-order single line mathematical execution, but from the way your question is written for you, I think your teacher wants recursion.)

what about the $words, where to they check in. for comparsion

    if($reverse == $word){ // compares the reverse layout of the word and the original word
     echo "T"; // when true, output 'T'
    }else{
     echo "F"; // when false, output 'F'
    }

You may want to brush up on the logic section of your programming textbook or section of the website you are attempting these from.

i understand the $words but where do i write words which i want to compare, like radar, apple etc.

You need to write a way to either 1) pass it in, or 2) collect it.

This has to have been covered somewhere in your class/tutorial right?

Nope, I thought it was suppose to be passed as an array in function. can it?

Okay, so since you need an array, you’ll also have to update the code, because currently it only works with a single word.

function Palindromes($words) {
    for ($i = 0; $i < size_of($words); $I++) {
        $word = $words[$i];
        $reverse = strrev($word);
        if($reverse == $word){
         echo "T";
        }else{
         echo "F";
        }
    }
}

//Call it
Palindromes({"noon","orange","wow"});

Seriously? You’re a bit lazy, don’t you think?

Yey… I found the formula http://mathproofs.blogspot.ro/2005/04/nth-term-of-fibonacci-sequence.html

<?php

if( false == isset($_GET['n']) ) {
	die('set n=...');
}

echo 'Fibo optimized: ' . fibo( $_GET['n'] );

function fibo( $n ) {
	return ( 1/sqrt(5) ) * ( pow((1+sqrt(5))/2, $n) - pow((1-sqrt(5))/2, $n) );
}


This is why i refuse to answer peoples’ homework for them.

Yey… I found the formula http://mathproofs.blogspot.ro/2005/04/nth-term-of-fibonacci-sequence.html

<?php

if( false == isset($_GET['n']) ) {
	die('set n=...');
}

echo 'Fibo optimized: ' . fibo( $_GET['n'] );

function fibo( $n ) {
	return ( 1/sqrt(5) ) * ( pow((1+sqrt(5))/2, $n) - pow((1-sqrt(5))/2, $n) );
}


There’s actually a simplification of that formula using rounding. Because of the way exponential functions work, the second term will always reduce (1-sqrt(5)/2 < 1. All X < 1, y In Integers, f(X) = x^y, f(x+1) < f(x)), and when x is limited to the positive integers, f(1) in this case = -0.6; f(2) = 0.3 (and from then on becomes rounding-insignificant) the formula can safely be reduced as: F(x) = round((1/sqrt(5))*pow((1+sqrt(5))/2,$x+1)), for the Xth term of the fibonacci set. (Note this is a 0-indexed set; so F(0) = 1, F(1) = 1, F(2) = 2, etc. You can remove the +1 from the power to achieve a 1-indexed set and retrieve the Xth Term of the Sequence as F(x) = round((1/sqrt(5))*pow((1+sqrt(5))/2,$x)) )

it might be easier for all of us if you just paste the list of words you want to check here and we’ll respond manually to the list. I know that you are needing help with this, but can you be bothered to do any of the work yourself?

I think this is an instance where we’re giving you fish rather than teaching you to fish. If you can’t understand simple functions, you aren’t ready for solving the Nth number in a fibonacci sequence