This code is displaying an INF value on the screen - php

The following code is displaying INF as the result. How can I fix it?
<?php
function fibonacci($n)
{
$a = 1;
$b = 1;
$result = 0;
for ($i = 0; $i < $n; $i=$i+1)
{
$sum = $a + $b;
$a = $b;
$b = $sum;
if ($a % 2 == 0)
{
$result = $result + $a;
}
}
echo "<br/>" . $result;
}
echo fibonacci(400000);
?>

The number is too big to display, and INF is a pretty good guess :) (fibonacci(1000) gives you a number with 210 digits).
100: 22 digits, 110: 24 digits, 120: 25 digits, 130: 27 digits
If you extrapolate that, you would end up with about (400000 / 10) * 2 = 80000 digits.

The following implements your logic using bcmath to prevent the INF error.
function fibonacci($n)
{
$a = '1'; $b = '1'; $result = '0';
for ($i = 0; $i < $n; $i++) {
$sum = bcadd($a,$b);
$a = $b;
$b = $sum;
if (bcmod($a,'2') == '0') {
$result = bcadd($result,$a);
}
}
echo "<br />".$result;
}
As your fibonacci function doesn't actually return any value, there's no point in echo fibonacci(400000)
EDIT
However, your logic is completely flawed. The following should give you the correct result for the problem you're trying to solve (again using bcmath):
function fibonacci($n)
{
$a = '0'; $b = '1'; $sum = '0';
$sum = '0';
do {
$fib = bcadd($a,$b);
$a = $b;
$b = $fib;
if (bccomp($fib,$n) == -1) {
if (bcmod($fib,'2') == '0') {
$sum = bcadd($sum,$fib);
}
}
++$i;
} while (bccomp($fib,$n) == -1);
return $sum;
}
echo fibonacci(4000000);
Rather than simply executing it to get the result, look to see how it works and what it's actually doing

Related

How to get the maximum possible sum of values of the edges’ endpoints php

The contents of this question have been removed due to a DMCA Takedown request by Codility Limited.
Here is the Simplets PHP solution for the Above question from the Codility test.
<?php
$A = [2,2,1,2];
$B = [1,3,4,4];
$w = [3,5,2,4,1];
$N = 5;
// $A = [1];
// $B = [3];
// $A = [1,3];
// $B = [2,4];
function solution ($N, $A, $B){
if(count($A) != count($B) || !is_int($N) )
{
return false;
}
$V = [];
$vertextCount = [];
foreach($A as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
foreach($B as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
if($vertextCount < $N)
{
$vertextCount[$N] = 0;
}
$VC = $vertextCount;
$tn = $N;
$wightArr = [];
while(count($VC) > 0){
$maxKey = current(array_keys($VC, max($VC)));
$wightArr[$maxKey] = $tn;
unset($VC[$maxKey]);
$tn--;
}
$sum = 0;
foreach($A as $k=>$val){
$sum += $wightArr[$A[$k]] + $wightArr[$B[$k]];
}
return $sum;
}
echo $sum = solution($N, $A, $B);
NOTE:- Tested against the 3 given Examples in the test, Not sure about all the test cases.

Divide whole numbers into X parts

I need to divide an integer value into x parts (dynamic) using php inside a for loop (Note:Both the number to be split and the split value are dynamic)
for eg: I have a value 127 and divide it into 2parts it would be 63 and 64.
$number = y; //for example is 127
$parts = x; //for example is 2
for($i=1;$i<$parts;$i++){
//first iteration should output 63
//second iteration should output 64 (the last iteration should be always higher is the $number is not divisible by $parts)
}
Check out this example. I use the modulo operator. This works whether it's an even or odd number. You could wrap this all in a function also.
$x = 127;
$a = 0;
$b = 0;
$a = floor($x/2);
$b = ($x % 2) + $a;
echo "A: " . $a . "| B: " . $b; //A: 63| B: 64
Try it in a function.
function remainders($x, $num) {
$results = array();
$firstOp = floor($x / $num);
for($a = 1; $a <= $num; $a++) {
if($a != $num) {
$results[] = $firstOp;
}
else {
if($x % 2 == 1) {
$results[] = $firstOp + 1;
}
else {
$results[] = $firstOp;
}
}
}
return $results;
}
Then you can iterate through the returned array or do what you want.
$splitNum = remainders(183, 4); //split the number 183 in 4 parts.
foreach($splitNum as $var) { echo $var . ", "; }
Try this:
$number = 127; //for example is 127
$parts = 3; //for example is 3
$sep = ", ";
$n=floor($number/$parts);
for($i=1;$i<=$parts;$i++){
if ($i==$parts) {
$n=$number-($n*($i-1));
$sep="";
}
echo $n.$sep;
}

How to find the common divisors of two numbers in PHP?

I use the following to find out the common divisors.
But in some case the count of divisors are not satisfied.
My Code :
$x = 66928;
$y = 66992;
$c_a = [];
$c_b = [];
$d = 1;
while ($d_a <= $x) {
if (is_int($x / $d)) $c_a[] = $d;
$d++;
}
$d = 1;
while ($d_b <= $y) {
if (is_int($y / $d)) $c_b[] = $d;
$d++;
}
echo count($c_a);
echo count($c_b);
// Output
$c_a = 20;
$c_b = 20;
Because, in some cases, it won't work.
Is this type of calculation is right ?
or any suggestions ?
As per asked in comment, to count the common factors of the two no. will be as like this.
<?php
$a = 66928;
$b = 66992;
$min = ($a < $b ) ? $a : $b;
$commomn_factors_count = 0;
for ($i = 1; $i < $min/2; $i++) {
if (($a%$i==0) && ($b%$i==0)) {
$commomn_factors_count++;
}
}
var_dump($commomn_factors_count);
You can you this code to get the fastest result to find the number of common divisors between two numbers:
// Function to calculate gcd of two numbers
function gcd($a, $b)
{
if ($a == 0)
return $b;
return gcd($b % $a, $a);
}
/* Function to calculate all common
* divisors of two given numbers
* a, b --> input integer numbers
*/
function commDiv($a, $b)
{
// find gcd of a, b
$n = gcd($a, $b);
// Count divisors of n.
$result = 0;
for ($i = 1; $i <= sqrt($n);
$i++)
{
// if 'i' is factor of n
if ($n % $i == 0)
{
// check if divisors
// are equal
if ($n / $i == $i)
$result += 1;
else
$result += 2;
}
}
return $result;
}
// Driver Code
$a = 10; $b = 15;
echo(commDiv($a, $b));

undefined offset PHP error in looping

it says an error undefined offset i dont know what causes it
im trying to shuffle $numberarray without repeating the number
heres the code
$numberarray = array(1,2,3,4,5,6,7,8,9,10);
for($counter=0;$counter<=9;$counter++)
{
$b = $counter - 1;
$a = $numberarray[$counter];
$numberarray[$counter] = rand(1,10);
do
{
$numberarray[$counter] = rand(1,10);
while($b != 0)
{
if($numberarray[$counter] == $numberarray[$b])
{
$numberarray[$counter] = rand(1,10);
$b = $counter - 1;
//echo $b;
}
else
{
$b--;
}
}
}while($a == $numberarray[$counter]);
echo $numberarray[$counter].", ";
}
sample output $numberarray = {3,4,5,1,2,7,9,10,8,5}
This would be better:
(By the way you don't need an extra condition before the while since while act like a condition itself)
$numberarray = array(1,2,3,4,5,6,7,8,9,10);
for($counter=0;$counter<=9;$counter++)
{
$b = $counter - 1;
$a = $numberarray[$counter];
$numberarray[$counter] = rand(1,10);
do
{
$numberarray[$counter] = rand(1,10);
while($b >0)
{
if($numberarray[$counter] == $numberarray[$b])
{
$numberarray[$counter] = rand(1,10);
$b = $counter - 1;
//echo $b;
}
else
{
$b--;
}
}
}while($a == $numberarray[$counter]);
echo $numberarray[$counter].", ";
}
Even if there is no more error, your code repeat numbers sometimes, so I would symply do it like this using shuffle:
$numberarray2 = array(1,2,3,4,5,6,7,8,9,10);
shuffle($numberarray2);
print_r($numberarray2);
The problem is that in the first $b is equal to -1 that's why you get the error so i think you should delete the if condition and edit the while statement to while($b!=-1).

How to find the highest combination while comparing three numbers in php

There are three numbers in set A {3,4,7} and in set B {2,4,7}.
It is not possible to get the result true because the the first numbers in a and b are not same.
But I need to get the result as true by comparing other two number and leaving the first number.
How is it possible to do it in PHP?
try this it will helps you.. it will display the 1 => for more number of combinations as same and 0 => for more of combination as different . in the below code the more number of combination as different so its returns 0.
<?php
$A = array(2,3,7,5,6);
$B = array(4,3,7,8,9);
$flagTrue = 0;
$flagFalse = 0;
for($i=0; $i < count($A); $i++)
{
if($A[$i] == $B[$i])
{
$flagTrue=$flagTrue+1;
}
else
{
$flagFalse=$flagFalse+1;
}
}
$var_is_greater_than_two = ($flagTrue >= $flagFalse) ? 1 : 0;
echo $var_is_greater_than_two;
?>
<?php
$a = array(3,4,7);
$b = array(2,4,7);
echo $a === $b ? 'TRUE' : 'FALSE';
echo PHP_EOL;
array_shift($a);
array_shift($b);
echo $a === $b ? 'TRUE' : 'FALSE';
?>
Shows:
FALSE TRUE
UPD:
If you need to extract values from strings, then:
$strA = '3,4,7';
$strB = '2,4,7';
$a = explode(',', $strA);
$b = explode(',', $strB);
array_shift($a);
array_shift($b);
echo $a === $b ? 'TRUE' : 'FALSE';
Should work.
<?
$A = array(2,3,7);
$B = array(4,3,7);
$isTrue=1;
for($i=1; $i < count($A); $i++) if($A[$i]!=$B[$i]) $isTrue=0;
echo $isTrue;
?>
EDIT:
If you want to return true if exactly two elements are the same, then the code would be:
$common=0;
for($i=0; $i < count($a); $i++) if($a[$i]==$b[$i]) $common++;
if($common==2) $isTrue=1;
function compareSets($a, $b) {
$result = TRUE;
$diffArray = array_diff($a, $b);
foreach ($diffArray as $key => $value) {
if ($key > 0) {
$result = FALSE;
break;
}
}
return $result;
}
<?php
$a = array(3,4,7);
$b = array(2,4,7);
for($i=0;$i<3;$i++)
{
if($a[$i] > $b[$i]
echo true;
}
?>
will give true if a is greater.

Categories