Jump to content

[PHP] Basic arithmetic help


Go to solution Solved by donatastommy,

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/282567-php-basic-arithmetic-help/
Share on other sites

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

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 by PaulRyan

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

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.