PHP script that calculates powers that are < X - php

I have a problem with my PHP script for calculating powers.
$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512
for ($i = 1; $A <= $B; $i++) {
echo $i . ". ";
echo pow($A, $i);
echo '<br>';
}
With the example i mentioned above, i know this script would still display 1024 but the problem is it doesn't work at all. I tried doing this many other ways and nothing works for me... please any guidance is really appreciated.

Let's take the example of $A = 2; $B = 10; so you'd expect to get 2, 4, 8.
Your for loop has as condition, execute as long as $A <= $B.
Let us manually do some iterations:
Run 1: $A: 2, $B: 10, ($A <= $B): true, $i: 1, output: "1. 2<br>"
Run 2: $A: 2, $B: 10, ($A <= $B): true, $i: 2, output: "1. 4<br>"
Run 3: $A: 2, $B: 10, ($A <= $B): true, $i: 3, output: "1. 8<br>"
Run 4: $A: 2, $B: 10, ($A <= $B): true, $i: 4, output: "1. 16<br>"
Run 5: $A: 2, $B: 10, ($A <= $B): true, $i: 5, output: "1. 32<br>"
Run X: $A: 2, $B: 10, ($A <= $B): true, $i: X, output: "1. 2^X<br>"
As we can see, the tested condition $A <= $B is always true. How to fix this? One option is to update variable $A, like this:
$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512
for ($i = 1; $A <= $B; $i++) {
$A = pow($A, $i);
if ($A <= $B) {
echo $i . ". ";
echo $A;
echo '<br>';
}
}
Another option would be to use a break condition:
$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512
for ($i = 1; true; $i++) {
$A = pow($A, $i);
if ($A > $B) {
break;
}
echo $i . ". ";
echo $A;
echo '<br>';
}
This is an infinite loop, which usually gets written as
$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512
$i = 1;
while (true) {
if ($A > $B) {
break;
}
echo $i . ". ";
$A = pow($A, $i);
echo $A;
echo '<br>';
$i++;
}
One last thing, since $A and $B get submitted by a client, you shouldn't assume they're numbers. You can simply cast them to numbers using:
$A = (int) $_POST['a'];
$B = (int) $_POST['b'];
How I personally would write that code:
// get variables
$base = (int) $_POST["a"];
$max = (int) $_POST["b"];
// validate input
if ($max < $base) {
echo 'error: Max number bigger than base number!<br>';
} elseif ($base < 2) {
echo 'error: Base number needs to be >= 2!<br>';
}
/** SOLUTION 1 - using `pow` */
// starting exponent
$exp = 1;
// assign $base^$exp to $result and test it against $max
while (($result = pow($base, $exp)) <= $max) {
// echo current row
echo $exp.'. '.$result.'<br>';
// increment exponent
$exp++;
}
/** SOLUTION 2 - using dynamic programming instead of calling `pow` in each iteration */
// multiply $result with the base and check against $max
for ($result = 1, $exp = 1; ($result *= $base) <= $max; $exp++) {
echo $exp.'. '.$result.'<br>';
}
/** In case you don't need the exponent:
for ($result = 1; ($result *= $base) <= $max;) {
echo $result.'<br>';
}
*/
Try to pick variables that explain what they're doing and add comments to explain what's happening.

$C = 1;
for ($i = 1; $C <= $B; $i++) {
$C = pow($A, $i);
if ($C <= $B) {
echo $i . ". " . $C . '<br>';
} else {
break;
}
}
Just added a variable $C that changes each time the loop runs so it can actually finish.

Related

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));

PHP for loop with exception rules

I am searching for a php for loop, which should output
22,23,24, 32,33,34, and so on 92,93,94 and all following like 102,103,104,122,123 until 902,903,904,992,993,9949, but not or 12,13,14... 112,13,114 ...912,913,914.
my code:
for ($a = 22; $a <= 1000; $a+=10) {
$b = $a + 1;
$c = $a + 2;
echo "$a <br>";
echo "$b <br>";
echo "$c <br>";
}
I need here an exception, because 112,123 and 124 and so on until 912,913,914 shouldn't be echoed.
All results should be stored in an array.
Try using this code:
$data = array();
for ($a = 2; $a < 10; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
$data[] = $c;
}
}
I have found all parts do finish kwestgrounds answer with my exceptions.
$delete = array_merge(range (112, 912, 100), range (113, 913, 100),range (114, 914, 100));
$data = array();
for ($a = 2; $a < 100; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
{
if (in_array($c, $delete)) continue;
}
$data[] = $c;
}
}

Loop not working correctly (floats and integer issue?) -- PHP [duplicate]

This question already has answers here:
Double? Integer? -- PHP
(2 answers)
Closed 8 years ago.
The code below generates two random decimal values, then subtracts them to get $c.
The do-while loop is trying to ensure that $c will not be a whole number. But I keep getting times where $c actually is a whole number.
do{
unset($a);
unset($b);
unset($c);
unset($adjuster);
unset($c_is_int);
$a = mt_rand(5, 75);
$b = mt_rand(5, 75);
$adjuster = mt_rand(2, 20);
$decimal_selector = mt_rand(1, 6);
if ($decimal_selector == 1){
$a = $a / 10;
$b = $b / 10;
}
if ($decimal_selector == 2){
$a = $a / 10;
$b = $b / 100;
}
if ($decimal_selector == 3){
$a = $a / 100;
$b = $b / 10;
}
if ($decimal_selector == 4){
$a = $a / 100;
$b = $b / 100;
}
if ($decimal_selector == 5){
$a = $a / 1000;
$b = $b / 1000;
}
if ($decimal_selector == 6){
$a = $a / 1000;
$b = $b / 100;
}
if($b < $a){
$b = $b + ($a - $b) + $adjuster;
}
$c = $b - $a;
if(intval($c) == $c) {
$c_is_int = 1;
} else {
$c_is_int = 0;
}
echo $a . '<br><br>';
echo $b . '<br><br>';
echo intval($c) . '<br>';
echo $c_is_int . '<br>';
echo $c . '<br><br>';
} while($c_is_int == 1);
The attached image shows the results of one of these failing times. Any ideas on where this is going wrong?
Why not check to see if the number has a decimal?
$c = 123.456;
if(strpos((string) $c, '.') !== FALSE) {
// is decimal/float/double
}
You can also just check to see if it's an int
if(is_int($c))
You want to keep in mind that the integer 3 and float 3.0 will compare as equal when using the ==.
Consider:
$c_is_int = $c == floor( $c );
...
while( $c_is_int )

Do-While Loop with Multiple Conditions

Can a do-while loop have multiple conditions? If so, I can't figure out why the code below is failing on all but the first condition.
Functions used...
function gcf($a,$b) {
$a = abs($a); $b = abs($b);
if( $a < $b) list($b,$a) = Array($a,$b);
if( $b == 0) return $a;
$r = $a % $b;
while($r > 0) {
$a = $b;
$b = $r;
$r = $a % $b;
}
return $b;
}
function factors($n){
$factors_array = array();
for ($x = 1; $x <= sqrt(abs($n)); $x++)
{
if ($n % $x == 0)
{
$z = $n/$x;
array_push($factors_array, $x, $z);
}
}
return $factors_array;
}
Code...
$a = $b;
do{
$a = mt_rand(8, 100);
$a_factors_array = factors($a);
$b = mt_rand(8, 100);
$b_factors_array = factors($b);
} while ($a == $b && count($a_factors_array) < 4 && count($b_factors_array) < 4 && gcf($a, $b) == 1);
echo $a . '<br>';
echo $b . '<br>';
echo count($a_factors_array) . '<br>';
echo count($b_factors_array) . '<br>';
echo gcf($a, $b) . '<br>';
I keep getting numbers for $a and $b that have less than 4 factors and have a GCF of 1. Any ideas?
You'll need || instead of &&. You want to repeat the loop as long as any one of your conditions is met. Currently the loop is only repeated if all of the conditions are met.
I think you have an ANDs where you meant an ORs:
do{
$a = mt_rand(8, 100);
$a_factors_array = factors($a);
$b = mt_rand(8, 100);
$b_factors_array = factors($b);
} while ($a == $b || count($a_factors_array) < 4 || count($b_factors_array) < 4 || gcf($a, $b) == 1);
With your way, the while stops if $a !== $b which is probably not what you want.

This code is displaying an INF value on the screen

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

Categories