so the task I'm trying to complete here is comparing a user input number against the fibonacci sequence and if it is a fibonacci number the program will echo true, if not it will echo false.
Can someone tell me where I'm going wrong?
this is my first file:
<?php
function print_fibonacci($n){
$first = 0;
$second = 1;
echo "Fibonacci Series: \n";
echo $first.' '.$second.' ';
for($i=2;$i<$n;$i++){
$third = $first + $second;
echo $third.' ';
$first = $second;
$second = $third;
}
}
/* Function call to print Fibonacci series upto 6 numbers. */
print_fibonacci(16);
?>
<form method="POST" action="fibonacci3.php"><br>
<label>Input a number to check if it is fibonacci or not.</label><br>
<input name="fib" type="text" placeholder="#" /><br>
<input type="submit" value="OK!" />
</form>
This outputs the fibonacci sequence until the 16th fibonacci number, and is followed by a form which allows a user to enter a number.
The next file is fibonacci3.php as referenced in the form.
<?php
include "fibonacci2.php";
$n = $_POST["fib"];
function fibonacci($n) {
//0, 1, 1, 2, 3, 5, 8, 13, 21
/*this is an error condition
returning -1 is arbitrary - we could
return anything we want for this
error condition:
*/
if((int)$n <0){
return -1;
echo "False";
}
if ((int)$n == 0){
return 0;
echo "0";
}
if((int)$n == 1 || $n == 2){
return 1;
}
$int1 = 1;
$int2 = 1;
$fib = 0;
for($i=1; $i<=$n-2; $i++ )
{
$fib = $int1 + $int2;
//swap the values out:
$int2 = $int1;
$int1 = $fib;
}
if ($fib = $int1 + $int2 && $n == $fib){
echo "True!";
} else {
echo "False!";
}
return $fib;
}
fibonacci((int)$n);
?>
I thought this might be correct but it's not outputting anything when the user inputs a number.
$n = 'Your number';
$dRes1 = sqrt((5*pow($n, 2))-4);
$nRes1 = (int)$dRes1;
$dDecPoint1 = $dRes1 - $nRes1;
$dRes2 = sqrt((5*pow($n, 2))+4);
$nRes2 = (int)$dRes2;
$dDecPoint2 = $dRes2 - $nRes2;
if( !$dDecPoint1 || !$dDecPoint2 )
{
echo 'True';
}
else {
echo 'False';
}
Related
<?php
if (isset($_GET['number'])){ // Loaded for first time?
if(isValid($_GET['number'])){
$isPrime = true;
// Please enter code here
// I have tried the codes here but idk why it cannot seem to work
$i = $_GET(['number']);
if ($number == 1) {
$isPrime = false;
return $isPrime;
}
for ($j = 2; $i <= $number/2; $i++){
if ($number % $i == 0)
$isPrime = false;
return $isPrime;
}
return $isPrime;
// End of code
if ($isPrime) {
echo "<p>".$i." is a prime number!</p>";
} else {
echo "<p>".$i." is not prime.</p>";
}
} else{
// User submitted something which is not a positive whole number
echo "<p>Please enter a positive whole number.</p>";
}
}
// check if the given number is a valid numeric value
// round() rounds a floating point value
function isValid($number) {
if(is_numeric($number) && $number > 0
&& $number == round($number, 0)) {
return true;
}
else {
return false;
}
}
?>
I have tried the above codes, however, I have encountered issues. Please help me out thank you! The codes I have tried are within the // Please enter code here to // End of code
When I run the php file the above image will be shown. The user have to type a number and it will check whether or not the user input is a prime number when user clicks on the "Go!" button.
You had many mistakes.
So I edited the whole code.
here it is:
<?php
if (isset($_GET['number'])) { // Loaded for first time?
if (isValid($_GET['number'])) {
$isPrime = true;
// Please enter code here
// I have tried the codes here but idk why it cannot seem to work
$i = $_GET['number'];
if ($i == 1) {
$isPrime = false;
}
for ($j = 2; $j <= $i / 2; $j++) {
if ($i % $j == 0)
$isPrime = false;
}
// End of code
if ($isPrime) {
echo "<p>" . $i . " is a prime number!</p>";
} else {
echo "<p>" . $i . " is not prime.</p>";
}
} else {
// User submitted something which is not a positive whole number
echo "<p>Please enter a positive whole number.</p>";
}
}
// check if the given number is a valid numeric value
// round() rounds a floating point value
function isValid($number)
{
if (is_numeric($number) && $number > 0
&& $number == round($number)) {
return true;
} else {
return false;
}
}
?>
<form action="index.php" method="get">
<input type="number" name="number">
<input type="submit" value="Go!">
</form>
I'm having some issues getting the input code to work with the equation. I've been trying for a while (I'm new to coding) and searching, while not being able to make it work in any way. This is what I ended up with. Can anyone help me? Thanks in advance.
<!DOCTYPE HTML>
<html>
<body>
<form action="" method="post">
<br />Choose a number to be the nth in the Fibonacci sequence: <input type="number" name="fibnum">
<input type="submit">
</form> <br /> <br />
<?php
if(isset($_POST['fibnum'])){
$fibnum = $_POST['fibnum'];
function fibRec($fibnum){
if ($fibnum < 0){
echo "There are no Fibonacci numbers of negative values.";
}
elseif ($fibnum == 0){
echo "0";
}
elseif ($fibnum == 1){
echo "1";
}
else {
$sum = fibRec($fibnum-1)+fibRec($fibnum-2);
echo $sum;
}
}
$fib = fibRec($fibnum);
echo $fib;
}
?>
</body>
</html>
to make it work all you need to do is change the echo's to returns like so:
<?php
$_POST['fibnum']=11;
if(isset($_POST['fibnum'])){
$fibnum = $_POST['fibnum'];
function fibRec($fibnum){
if ($fibnum < 0){
return ( "There are no Fibonacci numbers of negative values.");
}
elseif ($fibnum == 0){
return ("0");
}
elseif ($fibnum == 1){
return ("1");
}
else {
$sum = fibRec($fibnum-1)+fibRec($fibnum-2);
return ($sum);
}
}
$fib = fibRec($fibnum);
echo $fib;
}
?>
working demo: http://ideone.com/e0xeY7
you were pushing integers into echo. in a recursive formula, returning the value goes back to the calling entity, not automatically outside the function.
Take a look at this:
<!DOCTYPE HTML>
<html>
<body>
<form action="" method="post">
<br />Choose a number to be the nth in the Fibonacci sequence: <input type="number" name="fibnum">
<input type="submit">
</form> <br /> <br />
<?php
if(isset($_POST['fibnum'])){
$fibnum = $_POST['fibnum'];
function fibRec($fibnum){
if ($fibnum < 0){
echo "There are no Fibonacci numbers of negative values.";
}
elseif ($fibnum == 0){
return 0;
}
elseif ($fibnum == 1){
return 1;
}
else {
$sum = fibRec($fibnum-1)+fibRec($fibnum-2);
return $sum;
}
}
$fib = fibRec($fibnum);
echo $fib;
}
?>
</body>
</html>
You can also use PHI constant associated with golden ratio to calculate nth fibonacci number. it is considered that fibonnaci number until 5th iteration are not as per golden ratio so you can have it like below :
<?php
$fibnum = intval($_POST['fibnum']);
const PHI = 1.6180339887;
$f = [0, 1, 1, 2, 3];
echo getNthFibo($fibnum);
function getNthFibo($i)
{
if($i < 5){
return $f[$i];
}
// Starting from 5th iteration of fibo function
$n = 5;
$fn = 3;
while ($n < $i) {
$fn = round($fn * PHI);
$n++;
}
return $fn;
}
So i have this problem where i generate random numbers from 1 - 10 then displaying the numbers then identifying what is repeating. BTW intNcases is the number of numbers and should not exceed 20. This is actually our assignment and i'm really having a hard time with it please help. This is my code so far.
Sample Output
Random numbers of case is: 7
Random numbers are: 4, 2, 1, 1, 4,3,2
Numbers: 4, 2, 1, 3
Repeating numbers are: 4, 2, 1
<html>
<body>
<?php
$intNcases = 5;
$hold = array(0,0,0);
$temp = array(0,0,0);
$rep = array(0,0,0);
$num = array(0,0,0);
$count = 1;
if($intNcases>20)
{
echo 'Error N cases is greater than 20';
}
else
{
echo 'The number of case/s is: '. $intNcases;
echo '<br><br>'. 'Input'.'<br>';
for($x=0;$x<$intNcases;$x++)
{
$N = rand(1,10);
echo $N. '<br>';
$hold[$x] = $N;
$temp[$x] = $N;
}
echo 'OUTPUT<br>';
for($d=0;$d<$intNcases;$d++)
{
for($j=1;$j<$intNcases;$j++)
{
if($hold[$d] == $temp[$j])
{
$rep[$j-1] = $hold[$j-1];
$hold[$j-1] = 0;
}
else
{
$num[$j-1] = $hold[$j-1];
}
}
echo '#'.$count.' - '.$num[$d]. '<br>';
$count++;
}
echo 'Repeating numbers are: ';
for($k=0;$k<sizeof($rep);$k++)
{
echo $rep[$k]. ' ';
}
}
?>
</body>
</html>
Maybe you can do this more easier with array_unique or array_shuffle or other array functions
You could try this.
$intcases = rand(1,20);
$numbers = array();
echo 'Random number of cases: '. $intcases . '</br>';
echo 'Random numbers are: ';
for($x=0;$x<$intcases;$x++)
{
$number = rand(1,10);
echo $number. ' ';
if(array_key_exists($number, $numbers))
{
$numbers[$number] = $numbers[$number] + 1;
}else
{
$numbers[$number] = 1;
}
}
echo '</br>';
echo 'Numbers are: ';
foreach($numbers as $number => $x)
{
echo $number . ' ';
}
echo '</br>';
echo 'Repeating numbers: ';
foreach($numbers as $key => $value)
{
if($value > 1)
{
echo $key . ' ';
}
}
echo '</br>';
This question already has answers here:
Create a mechanism to pass in a positive integer and display all the values of the Fibonacci series up to and including the specified value
(2 answers)
Closed 8 years ago.
How would I create a mechanism to pass in a positive integer and display the operand, the Fibonacci series number of the operand and the sum of all the values of the Fibonacci series up to and including the specified value?
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="main.css"/>
<title>Fib Activity 3</title>
</head>
<body>
<h1>Pick Category Using GET</h1>
Films
Music
Books
<br />
<h2>Fibonacci</h2>
<form method="get" action="fib3.php">
<fieldset>
<label for="powerof">Fibonacci: </label>
<input type="text" name="powerof" value="<?php echo $_GET['powerof']; ?>"/>
<input type="submit" name='Go' value="Calculate" />
</fieldset>
</form>
<?php
$message = 'The fibonacci sequence is: <br />1<br />2<br />';
$powerof = 0;
$max = 10;
$temp = $max;
if (isset($_GET['powerof'])) {
$powerof = $_GET['powerof'];
}
if ($powerof > 100) {
$powerof = 100;
$message = 'Sorry, your input was too high. I converted it to the maximum value of 100.<br />The fibonacci sequence is: <br />1<br />2<br />';
}
$x = 0;
$y = 1;
$z = 0;
$counter = 0;
while ($counter < $powerof) {
if ($counter <= 1) {
$z = 1;
} else {
$z = $x + $y;
}
echo ($z. "<br />");
$x = $y;
$y = $z;
$counter++;
}
?>
</body>
</html>
all help is appreciated
First off, you aren't quite getting the Fibonacci series correct. The first number in the Fibonacci series is 0. The script I wrote here assumes that $powerof needs to be at least 1 (this can be easily changed if you need).
I also wrote this using a recursive function which takes the number you are going to. the first number, and the second number. It will loop through by calling itself and each time, it will print out the fibonacci series at the number.
<?php
$powerof = 0;
if (isset($_GET['powerof'])) {
$powerof = $_GET['powerof'];
}
$message = 'The fibonacci sequence is:';
if ($powerof > 100) {
$powerof = 100;
$message = 'Sorry, your input was too high. I converted it to the maximum value of 100.<br />The fibonacci sequence is:';
}
echo $message;
$first = 0;
$second = 1;
if ($powerof > 1) {
echo '<br>0<br>1';
} else if ($powerof < 1) {
echo ' Invalid input';
} else {
fib($powerof, $first, $second);
}
function fib($powerof, $first, $second) {
if( $powerof > 0 ) {
$sum = $first + $second;
$first = $second;
$second = $sum;
echo $sum.'<br>';
fib($n-1, $first, $second);
}
}
?>
I hope this all makes sense, if you have any more questions, let me know
This question already has answers here:
More concise way to check to see if an array contains only numbers (integers)
(6 answers)
Closed 10 years ago.
I made a form that you can send numbers with for example
1 2 3 4 5 6 7 8 9 10
And then I made a PHP code that will grab these numbers inside a array & loop that will calculate how many array items there is and then do the calculations.
I used is_numeric to check if the array contains only ints.
But for some reason it doesn't really work.
<?php
$total = null;
$number = $_POST['number'];
$numbers = explode(" ", $number);
foreach($numbers as $number) {
$total = $total + $number;
}
$notnumber = '<center>You must enter a number</center>';
$empty = '<center>The field is empty.</center>';
if ($numbers == is_numeric($numbers) && $total != null) {
$avg = $total / $number;
echo '<center>Avarge is: <b>'.$avg.'</b></center>';
} else if ($_POST['number'] == "") {
echo $empty;
} else if ($numbers != is_numeric($numbers)) {
echo $notnumber;
}
?>
This is the form
<form action="index.php" method="post">
<input type="text" name="number" class="input"><br /><br />
<input type="submit" value="Calculate results">
</form>
What happens:
When I enter numbers, it will echo the error "$notnumbers" yet they are numbers.
What have I done wrong?
Thanks.
if($numbers == is_numeric($numbers) ... is not correct
use
if (is_numeric($numbers) && $total != null) {
The better function is ctype_digit
Please try this and do not use yours!
//$_POST['number'] = '1 2 3 4 5 6 7 8 9 10';
$notnumber = '<center>You must enter a number</center>';
$empty = '<center>The field is empty.</center>';
if(!isset($_POST['number']) || strlen($_POST['number']) <= 0){
echo $empty;
}
else{
if( !preg_match('/^([1-9]{1})([0-9]*)(\s)([0-9]+)/', $_POST['number']) ){
echo $notnumber;
}else{
$total = null;
$number = $_POST['number'];
$numbers = explode(" ", $number);
//remove after test
echo "<pre>";
print_r($numbers);
echo "</pre>";
//end remove
$total = array_sum($numbers);
//count(array) = number of elements inside $array
$avg = $total / count($numbers);
echo '<center>Avarge is: <b>'.$avg.'</b></center>';
}
}
This is incorrect: $numbers == is_numeric($numbers).
Alternate Solution:
You could try this, providing you count 0 as an invalid number.
$number = $_POST['number'];
$numbers = explode(" ", $number);
$total = 0;
foreach($numbers as &$number) {
// covert value to integer
// any non-numerics become 0
$number = (int) $number;
$total += $number;
}
Now you won't have to worry about checking for is_numeric or is_int because they'll all be ints. You'll just need to check for 0 values instead.
Extra Tip:
<center> tag is also deprecated in HTML.CSS should be used to display centred text.
try this i solve some problem on code
<?php
$total = NULL;
$number = #$_POST['number'];
$numbers = explode(" ", $number);
$Num=0;
foreach($numbers as $number) {
$total = $total + $number;
$Num++;
}
$notnumber = '<center>You must enter a number</center>';
$empty = '<center>The field is empty.</center>';
if (is_array($numbers) && $total != NULL) {
$avg = $total / $Num;
echo '<center>Avarge is: <b>'.$avg.'</b></center>';
} else if (!isset($_POST['number'])) {
echo $empty;
} else if (!is_numeric($number)) {
echo $notnumber;
}