posted 9 months ago
The problem I am trying to solve is this:
When the user provides a whole number, the program returns the next whole number which is a perfect square.
For example, if I provide 5, then the program will check if 6, or 7, or 8, or 9 are perfect squares. It will detect 9 as being a perfect square and tell you that 3 times 3 is 9.
My pseudocode is as follows:
1. take input as variable "n" of type int.
2. create a for loop which starts at 1 and runs till i = n.
3. the body of the for loop will calculate n/i and compare it with i for equality.
e.g. If the user passes 9, then the first iteration will be as follows:
9/1 = 9 (false)
second iteration:
9/2 = 4.5 (false)
third iteration:
9/3 = 3 (true, because i equals n)
4. The program will stop and tell us that "i" times "i" equals "n"
In this case, 3 times 3 equals 9
I wrote the following code, but I know that I have to account for modulus and probably use a float or double. I'm also not sure if my i == n should be i < n, instead.
Needless to say, my program does not work and I'm missing a few things
When the user provides a whole number, the program returns the next whole number which is a perfect square.
For example, if I provide 5, then the program will check if 6, or 7, or 8, or 9 are perfect squares. It will detect 9 as being a perfect square and tell you that 3 times 3 is 9.
My pseudocode is as follows:
1. take input as variable "n" of type int.
2. create a for loop which starts at 1 and runs till i = n.
3. the body of the for loop will calculate n/i and compare it with i for equality.
e.g. If the user passes 9, then the first iteration will be as follows:
9/1 = 9 (false)
second iteration:
9/2 = 4.5 (false)
third iteration:
9/3 = 3 (true, because i equals n)
4. The program will stop and tell us that "i" times "i" equals "n"
In this case, 3 times 3 equals 9
I wrote the following code, but I know that I have to account for modulus and probably use a float or double. I'm also not sure if my i == n should be i < n, instead.
Needless to say, my program does not work and I'm missing a few things
posted 9 months ago
What if you start with n=5? The answer should be 9 but your algorithm fails if n is not a perfect square.
If you want a brute force algorithm:
start with i = 1 as long as i * i <= n increment i answer is i * i
This works but would be very slow for large values of n.
A simpler algorithm that involves floating point:
i = square root of n truncated to an integer increment i answer = i * i
If you want a brute force algorithm:
This works but would be very slow for large values of n.
A simpler algorithm that involves floating point:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Vasyl Lyashkevych
Ranch Hand
Posts: 70
posted 9 months ago
Sorry, your code doesn't work. If n=9 then it goes into an infinite (mostly) loop. If n=5 then your code yields: 2 times 2 equals 6 (the answer is supposed to be 9). The very first time through i=1 then for all other iterations i will equal 2 and never anything more.
Vasyl Lyashkevych wrote:I already posted executed code, but we need to help. So, I pointed to that approaches, but didn't tested
Sorry, your code doesn't work. If n=9 then it goes into an infinite (mostly) loop. If n=5 then your code yields: 2 times 2 equals 6 (the answer is supposed to be 9). The very first time through i=1 then for all other iterations i will equal 2 and never anything more.
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Acetylsalicylic acid is aspirin. This could be handy too:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
