 Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12892 Members
Join this group to post and comment. Mahima Kohli • Oct 1, 2018

# Write a program to count how many integers from 1 to 1000 are not perfect squares, perfect cubes, or perfect fifth powers

how to get numbers that are not perfect squares or perfect cube or perfect fifth Kaustubh Katdare • Oct 2, 2018

I'll give you an algorithm and would like you to try writing the program on your own. If after trying you are unable to figure out the program, I'll show you how it's done. Okay?

Question: How do we find out whether a number is a perfect square?

Answer: Find out the square root of the number and check if it's an integer.

The same applies to any 'nth' root of a number. If it's an integer; you can say that it's a perfect nth power.

With that knowledge, the approach will be like this-

1. Run a loop 1000 times each time incrementing the number by 1.

2. In every loop, write multiple if conditions to check if it fits in the given criteria - that is, the nth root of the number (where n = 2, 3 or 5) is an integer. If it is not, the number fits in our criteria. Increment our final counter by 1.

3. When you exit out of the loop, you will have the final counter set to the count of numbers that are not nth roots of any number 2,3 or 5.

Give it a try. You will have to find out which functions are available in the programming language that you are using. Hint: Look for 'pow' function, which lets you calculate nth power of any number. If you want to use it to calculate square root, you will set 'n' to 1/2. For cube root, set it to 1/3.

I hope this works. Give it a try and paste your attempt below. If you are unable to figure out the solution, I'll post the answer.

Share this content on your social channels -