Given a number N. The task is to count total numbers under N which are both perfect square and cube of some integers.
Input: N = 100 Output: 2 They are 1 and 64. Input: N = 100000 Output: 6
Approach: For a given positive number N to be a perfect square, it must satisfy P2 = N Similarly, Q3 = N for a perfect cube where P and Q are some positive integers.
N = P2 = Q3
Thus, if N is a 6th power, then this would certainly work. Say N = A6 which can be written as (A3)2 or (A2)3.
So, pick 6th power of every positive integers which are less than equal to N.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- N-th number which is both a square and a cube
- Find smallest perfect square number A such that N + A is also a perfect square number
- Check if a number is a perfect square having all its digits as a perfect square
- Previous perfect square and cube number smaller than number N
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Construct an Array such that cube sum of all element is a perfect square
- Percentage increase in volume of the cube if a side of cube is increased by a given percentage
- Print N numbers such that their sum is a Perfect Cube
- Check if number formed by joining two Numbers is Perfect Cube
- Print N numbers such that their product is a Perfect Cube
- Count all triplets whose sum is equal to a perfect cube
- Count of pairs in an Array whose sum is a Perfect Cube
- Find all numbers up to N which are both Pentagonal and Hexagonal
- Count of numbers upto M divisible by given Prime Numbers
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count subsequences which contains both the maximum and minimum array element
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Check if a number is perfect square without finding square root
- Count square and non-square numbers before n
- Count numbers < = N whose difference with the count of primes upto them is > = K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.