# Count numbers upto N which are both perfect square and perfect cube

Given a number N. The task is to count total numbers under N which are both perfect square and cube of some integers.

**Examples:**

Input:N = 100Output:2 They are 1 and 64.Input:N = 100000Output:6

**Approach:** For a given positive number N to be a perfect square, it must satisfy P^{2} = N Similarly, Q^{3} = N for a perfect cube where P and Q are some positive integers.

N = P^{2} = Q^{3}

Thus, if N is a 6th power, then this would certainly work. Say N = A^{6} which can be written as **(A ^{3})^{2} or (A^{2})^{3}.**

So, pick 6th power of every positive integers which are less than equal to N.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return required count ` `int` `SquareCube(` `long` `long` `int` `N) ` `{ ` ` ` ` ` `int` `cnt = 0, i = 1; ` ` ` ` ` `while` `(` `int` `(` `pow` `(i, 6)) <= N) { ` ` ` `++cnt; ` ` ` `++i; ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `int` `main() ` `{ ` ` ` `long` `long` `int` `N = 100000; ` ` ` ` ` `// function call to print required answer ` ` ` `cout << SquareCube(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// Function to return required count ` ` ` `static` `int` `SquareCube(` `long` `N) ` ` ` `{ ` ` ` ` ` `int` `cnt = ` `0` `, i = ` `1` `; ` ` ` ` ` `while` `((` `int` `)(Math.pow(i, ` `6` `)) <= N) { ` ` ` `++cnt; ` ` ` `++i; ` ` ` `} ` ` ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` `long` `N = ` `100000` `; ` ` ` ` ` `// function call to print required answer ` ` ` `System.out.println(SquareCube(N)) ; ` ` ` `} ` ` ` `// This code is contributed by Ryuga ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the ` `# above approach ` ` ` `# Function to return required count ` `def` `SquareCube( N): ` ` ` ` ` `cnt, i ` `=` `0` `, ` `1` ` ` ` ` `while` `(i ` `*` `*` `6` `<` `=` `N): ` ` ` `cnt ` `+` `=` `1` ` ` `i ` `+` `=` `1` ` ` ` ` `return` `cnt ` ` ` `# Driver code ` `N ` `=` `100000` ` ` `# function call to print required ` `# answer ` `print` `(SquareCube(N)) ` ` ` `# This code is contributed ` `# by saurabh_shukla ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// Function to return required count ` ` ` `static` `int` `SquareCube(` `long` `N) ` ` ` `{ ` ` ` ` ` `int` `cnt = 0, i = 1; ` ` ` ` ` `while` `((` `int` `)(Math.Pow(i, 6)) <= N) { ` ` ` `++cnt; ` ` ` `++i; ` ` ` `} ` ` ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `long` `N = 100000; ` ` ` ` ` `// function call to print required answer ` ` ` `Console.WriteLine(SquareCube(N)) ; ` ` ` `} ` `} ` ` ` `/*This code is contributed by 29AjayKumar*/` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the ` `// above approach ` ` ` `// Function to return required count ` `function` `SquareCube(` `$N` `) ` `{ ` ` ` `$cnt` `= 0; ` ` ` `$i` `= 1; ` ` ` ` ` `while` `((pow(` `$i` `, 6)) <= ` `$N` `) ` ` ` `{ ` ` ` `++` `$cnt` `; ` ` ` `++` `$i` `; ` ` ` `} ` ` ` ` ` `return` `$cnt` `; ` `} ` ` ` `// Driver Code ` `$N` `= 100000; ` ` ` `// function call to print required answer ` `echo` `SquareCube(` `$N` `); ` ` ` `// This code is contributed by ita_c ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Count of subarrays having exactly K perfect square numbers
- Construct an Array such that cube sum of all element is a perfect square
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Print N numbers such that their sum is a Perfect Cube
- Count of pairs in an Array whose sum is a Perfect Cube
- Count all triplets whose sum is equal to a perfect cube
- Print N numbers such that their product is a Perfect Cube
- Check if number formed by joining two Numbers is Perfect Cube
- Previous perfect square and cube number smaller than number N
- Count of subarrays whose sum is a perfect square
- Print n numbers such that their sum is a perfect square
- Sum of all perfect square divisors of numbers from 1 to N
- Count of pairs in an array whose sum is a perfect square
- Count of pairs in an array whose product is a perfect square
- Find smallest perfect square number A such that N + A is also a perfect square number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.