Php taking more than 30 sec : Error!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psaha
Forum Newbie
Posts: 16
Joined: Mon Oct 26, 2009 4:11 am

Php taking more than 30 sec : Error!

Post by psaha »

Sum of squares of all even numbers of the array + Square of sum of all odd number in the numbers array:
It could not understand where is the error :(
If you have different approach please let me know..

Code: Select all

 <?
$numbers="2 51 12 32 10 5 88 3";
$number=explode(" ",$numbers);
for($i=0; $number[$i]!='\0';$i++)
{
$odd=($number[$i]%2);
echo $number[$i];
if($odd)
$oddarrold+=$number[$i];
else
    $evenarr+=($number[$i]*$number[$i]);
 
} 
$oddarr=$oddarrold*$oddarrold;
echo ($oddarr+$evenarr);
?>
 
please help.....
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Php taking more than 30 sec : Error!

Post by requinix »

Code: Select all

$number[$i]!='\0'
That may work in C but it doesn't work in PHP.

Use a foreach instead of a for.
psaha
Forum Newbie
Posts: 16
Joined: Mon Oct 26, 2009 4:11 am

Re: Php taking more than 30 sec : Error!

Post by psaha »

Code: Select all

 
$number=explode(" ",$numbers);
foreach($number as $value)
{
$odd=($value%2);
if($odd)
$oddarrold+=$value;
else
    $evenarr+=($value*$value);
 
} 
$oddarr=$oddarrold*$oddarrold;
echo ($oddarr+$evenarr);
 
Now it is working . Thanks...
Post Reply