Difficulties with is_int function in PHP - php

I'm writing a code to solve a problem.one of my functions is not working properly.
function check_factor($sqr,$num)
{
for($i = $sqr ; true ; $i++)
{
$n = pow($sqr,2) - $num;
$s = sqrt($n);
if(is_int($s))
{
return $i;
}
}
}
I know that $s is a "double" , but even when I limit my loop counter to 2,I'll get an endless loop.
What am I missing here? why the function doesnt simply return null? and why I get Infinite loop even when there are 2 iterations?

true is making it an infinite loop
you will always get an infinit loop because sqrt() returns float means
$s = sqrt($n);
$s is a float now
and your test is testing if $s is an integer so it will be an infinit loop even after $i=2 the loop will always stuck and the $i=2 in every loop but
if you change the code to this
<?php
function check_factor($sqr,$num)
{
echo "<br>";
for($i = 1 ; $i < 3 ; $i++)
{
echo " in the loop $i<br>";
$n = pow($sqr,2) - $num;
echo "$n<br>";
$s = sqrt($n);
echo "$s<br>";
if(is_int($s))
{
echo "in the if <br>";
return $i;
}
}
return 0;
}
$val=0;
$val = check_factor(5,2);
echo "<br>$val<br>";
?>
the out put should be like this
in the loop 1
23
4.7958315233127
in the loop 2
23
4.7958315233127
0
and that's it. i hope i helped.

According to the manual: PHP function sqrt() always returns a float and NEVER an integer.
Check http://php.net/manual/en/function.sqrt.php

Related

How to compare 2 numeric strings

I have 2 strings like this
$s1="32.56.86.90.23";
$s2="11.25.32.90.10";
I need to compare $s1 and $s2 and find if there are 2 or more numbers in common.
I am using this way
$s1_ar=explode(".",$s1);
$s2_ar=explode(".",$s2);
$result=array_diff($s1_ar,$s2_ar);
$rt1=5-count($result);
if($result>=2){ echo "YES"; } else {echo "no"; }
Since I need millions values of $s1 and $s2 and the code above seems to be slow, do you know alternative way to execute the work faster ?
I tested it with the following code, one million times, less than 2 seconds on my 3 years old laptop.
Loop 1M times takes no time, most time is used for displaying.
Comment off the display, 1M loops, 0.816432 seconds
Saved the results into a file, ~13.564MB, 0.731708 seconds
ob_start();
$t1 = microtime();
for($i=1; $i<=1000000; $i++) {
$s1="32.56.86.90.23";
$s2="10.25.30.90.10";
$s1_ar=explode(".",$s1);
$s2_ar=explode(".",$s2);
$result=array_diff($s1_ar,$s2_ar);
$rt1=5-count($result);
if($result>=2){ echo $i . " YES<br>"; } else {echo $i . " no<br>"; }
}
$out = ob_get_contents();
ob_end_clean();
var_dump($out);
echo '<p>'.(microtime() - $t1).'</p>';
Try this.
$s1="32.56.86.90.23";
$s2="11.23.32.90.10";
$s1_ar=explode(".",$s1);
$s2_ar=explode(".",$s2);
//assuming $s1_ar and $s2_ar both has unique values if not please make them unique
$result_array = array();
$hasMatch = 0;
for($i = 0; $i < count($s1_ar) && $i < count($s2_ar); $i++){
if(!isset($result_array[$s1_ar[$i]])){
$result_array[$s1_ar[$i]] = 1;
}else{
$result_array[$s1_ar[$i]]++;
}
if(!isset($result_array[$s2_ar[$i]])){
$result_array[$s2_ar[$i]] = 1;
}else{
$result_array[$s2_ar[$i]]++;
}
}
foreach($result_array as $result){
if($result >=2) $hasMatch++;
}
if($hasMatch >= 2)
echo "YES";
else
echo "NO";
I think it will solve your purpose.
Looking at: php array_intersect() efficiency
There's mention that array_intersect_key may be more efficient. But really it would be nice to have data and versions to compare results.
$s1 = "2.3.5.7.9.11.13.17";
$s2 = "2.3.4.5.6";
$s1 = array_flip(explode('.', $s1));
$s2 = array_flip(explode('.', $s2));
echo count(array_intersect_key($s1, $s2))>=2 ? 'yes' : 'no';
Output:
yes
I thought of a way to solve this in a 2*n complexity:
We loop one list and create an associative array from it's elements (LIST c) then we loop the second list and look up if the list c contains such an index/key ( c[element] ).
This shall be very light weighted :
$commons = 0;
$s1_fliped = array_flip($s1_ar)
foreach($s2_ar as $s2_el){
if ( isset($s1_fliped[$s2_el]) ){
$commons ++;
}
if($commons >=2) break;
});

To show looping for two looping in random and cross

i have looping problem, i want To show replay random To looping
Code:
for ($i=0; $i <= 10; $i++) {
$result = $i;
echo "$result";
echo "<br>";
}
//----------------------------------------------
echo "<hr width='100%'>";
//----------------------------------------------
$s=$result;
$c=$result;
$test=array_fill($s, $c, 0);
$ts=microtime(true);
for ($i=0; $i <= 10; $i++) {
$selection1=mt_rand($s, $c);
$selection2=mt_rand($s, $c);
echo "$selection1 and $selection1";
echo "<br>";
}
i want result:
example result
and after random i want cross looping
Example result
#including completion of a genetic algorithm
mt_rand returns a value between the first and last values you provide. In your code, you are calling mt_rand($s, $c) when $s and $c are both set to 10, so they will always return 10.
I'm having trouble understanding what you want, so let's take a look at your code and see what's going on.
for ($i=0; $i <= 10; $i++) {
$result = $i;
echo "$result";
echo "<br>";
}
Above we're looping between 0 and 10, each time around storing our loop number as $result.
//----------------------------------------------
echo "<hr width='100%'>";
//----------------------------------------------
$s=$result;
$c=$result;
$test=array_fill($s, $c, 0);
$ts=microtime(true);
Our loop has finished and we're moving on. This code is executed once. Since $result was 10 at the end of the loop, $s and $c will always be set to 10.
for ($i=0; $i <= 10; $i++) {
$selection1=mt_rand($s, $c);
$selection2=mt_rand($s, $c);
echo "$selection1 and $selection1";
echo "<br>";
}
We're now going to loop between 0 and 10 again. mt_rand() will always return a value between $s and $c, which will always be set to 10 and 10, so will always be 10. We're just going to echo 10 and 10 10 times.
I'm unsure what you're looking for so I'll just leave that explanation there.

For LOOP taking time to execute in PHP

I am taking a demo test at codility.com.
I tried the following PHP test code:
function solution($A) {
$min = 0;
$size = count($A)-1;
for($i=0;$i<5;$i++){
if($i=0)
$min=$A[0];
}
return $min;
}
The script takes around 3.03s to execute where they have set the maximum execution time to 2.00s.
And if i comment the FOR LOOP it works properly.
Any idea ?
You are overwritting your $i variable here:
if($i=0)
it should be
if($i==0)
because you have wirte if($i=0) it is assignment operator not comparision operator. make it correct if($i==0)
function solution($A) {
$min = 0;
$size = count($A)-1;
for($i=0;$i<5;$i++){
if($i==0)
$min=$A[0];
}
return $min;
}
Your for loop looks like this:
for($i = 0; $i < 5; $i++)
This means: initialize $i to 0, and until $i is 5 or more, execute the loop and increment $i.
But, you wrote this:
if($i = 0)
To compare $i and 0, you should've used ==, not =. This sets $i to 0. The if is not executed, as 0 is equal to false. Then $i is incremented to 1. 1 is less than 5, so the loop is executed forever.
Use if($i == 0) to fix it.
There is a mistake that everyone has pointed out which is if($i=0) should be if($i==0).
But I have one concern. Why there is a for loop when it is simply returning $min which is $A[0] ?

How to get a total value from a loop result for integers in php. without Javascript of Jquery

how to add a total of the numbers displayed as an integer from a loop. the loop result is not an increment. here is the problem.
<?php
$i = 0;
$num =1;
while($i<=4){
echo $num;
$i++;
}
echo $num;
?>
So the result is something like this.
1 1 1 1
so my problem is how can i total the result which should be 4, and save it in a variable without incrementing. and even more confusing is. how to do the same when the value of $num is dynamic. Thank you very much. in advance
Just make an array then sum them:
<?php
$i = 0;
$num =1;
while($i<=4){
$nums[] = $num;
$i++;
}
echo array_sum($nums); //Outputs 5
?>
This assumes that $num is always numeric.
Alternatively, you could just iterate an output variable, based on the value of $num. Like so:
<?php
$num = 1; // or 2, or 3, or 4 or whatever
$output = 0;
for ($i=0; $i<=4; $i++) {
$output += $num;
}
echo $output;
?>

PHP Explode function. If function

Could someone help suggest in below php explode function, we are displaying script after 5th listing. How is it possible to display script exactly after 5th listing and 10th listing on a page which has more than 10 listings
We tried using
if ($i == 5 & $i== 10)
but it does not work
Below is original code - which displays script after 5th listing
<?php
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
for($i = 0; $i < $numberOfListings; ++$i)
{
if ($i == 5)
{ ?>
<script> </script>
<?php }
echo $listings[$i] . "<hr/>";
}
?>
Edit
How is it like - if have to display a separate script on $i==9, could you advise.
Because $i starts at 0 (0 to 9 is 10, whilst 0 to 10 is 11). Try if ($i == 4 || $i== 9), with an or operator.
Also I would not use the && (the and operator), because it is unlikely $i will ever equal both 4 and 9. I'd suggest you read into Truth Tables (and maybe Propositional Calculus) because from seeing what you had tried originally, it would be helpful to understand how a truth table works.
(source: wlc.edu)
You can use the contine, continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
$arr = range(0,9);
foreach($arr as $number) {
if($number < 5) {
continue;
}
print $number;
}
Ref: http://php.net/manual/en/control-structures.continue.php
Try using modulus operator
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
for($i = 1; $i < $numberOfListings; ++$i)
{
if ($i%5 == 0)
{
echo "in";
?>
<script> </script>
<?php
}
echo $listings[$i-1] . "<hr/>";
}
Here we are looping from 1 and there for $i <= $numberOfListings
and while listing we will use $listings[$i-1]
DEMO CODE AT http://codepad.viper-7.com/lrTOgP

Categories