donatastommy Posted September 30, 2013 Share Posted September 30, 2013 My task: You currently have $20000 in your bank account. You are to count the number of perfect squares from 1 to your amount. So first of all, I am trying to print out all the perfect squares (1,4,9,16, etc). That's what I want to do first, and I'm having trouble with it. My code: <?php $x=0; while($x<=20000) { $sqrt = sqrt($x); if($x == $sqrt * $sqrt) {echo $x . "</br>";} $x++; } ?> But all it does is print this, all the way to 1999: 0 1 4 9 11 14 16 17 21 22 25 27 30 33 34 35 36 39 41 42 44 46 47 49 53 54 55 56 57 62 64 67 68 69 70 71 74 79 81 83 84 85 86 88 90 91 93 98 99 100 101 103 108 110 114 118 120 121 126 129 130 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 30, 2013 Share Posted September 30, 2013 Because you're echo'ing out the sum each time. Quote Link to comment Share on other sites More sharing options...
donatastommy Posted September 30, 2013 Author Share Posted September 30, 2013 (edited) Because you're echo'ing out the sum each time. im not adding anything Edited September 30, 2013 by donatastommy Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 30, 2013 Share Posted September 30, 2013 (edited) Sorry meant to say the value of $x each time on line 7. Thats why you get long list of numbers. Also not need to spam your post with an image Edited September 30, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
donatastommy Posted September 30, 2013 Author Share Posted September 30, 2013 Sorry meant to say the value of $x each time on line 7. Thats why you get long list of numbers. Also not need to spam your post with an image Sorry Can you explain a little more if($x == $sqrt * $sqrt) {echo $x . "</br>";} im only echoing $x if its == $sqrt * $sqrt so thats conditional Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted September 30, 2013 Share Posted September 30, 2013 (edited) This is quite simple, you should look at pow to complete this. See the following: <?PHP //### Start at 1 $currentNumber = 1; //### While true, continuos till break is hit while(true) { //### Multiply the number by itself $number = pow($currentNumber, 2); //### If the number is less than 20000 display it //### If the number is greater than 20000, break the while loop if($number < 20000) { echo $number .'<br>'; } else { break; } //### Add 1 to the current number $currentNumber++; } ?> Edited September 30, 2013 by PaulRyan Quote Link to comment Share on other sites More sharing options...
kicken Posted September 30, 2013 Share Posted September 30, 2013 Basically you're if condition is incorrect. What you wrote would technically be always true, and should echo out every number. The reason it doesn't is due to computer limitations when it comes to floating point values (rounding errors). Quote Link to comment Share on other sites More sharing options...
donatastommy Posted September 30, 2013 Author Share Posted September 30, 2013 This is quite simple, you should look at pow to complete this. See the following: <?PHP //### Start at 1 $currentNumber = 1; //### While true, continuos till break is hit while(true) { //### Multiply the number by itself $number = pow($currentNumber, 2); //### If the number is less than 20000 display it //### If the number is greater than 20000, break the while loop if($number < 20000) { echo $number .'<br>'; } else { break; } //### Add 1 to the current number $currentNumber++; } ?> I'll test this when I get home. I believe it should work, however, how would you do this without pow or gdb? Basically you're if condition is incorrect. What you wrote would technically be always true, and should echo out every number. The reason it doesn't is due to computer limitations when it comes to floating point values (rounding errors). Yeah, realized that when I was eating lunch lol Quote Link to comment Share on other sites More sharing options...
Barand Posted September 30, 2013 Share Posted September 30, 2013 You could replace $number = pow($currentNumber, 2); with $number = $currentNumber * $currentNumber; Quote Link to comment Share on other sites More sharing options...
Solution donatastommy Posted September 30, 2013 Author Solution Share Posted September 30, 2013 You could replace $number = pow($currentNumber, 2); with $number = $currentNumber * $currentNumber; Got it, should be working, thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.