Given a number **n**, the task is to print all the numbers less than or equal to **n** which are **perfect cubes** as well as the eventual sum of their digits is **1**.

**Examples:**

Input:n = 100

Output:1 64

64 = 6 + 4 = 10 = 1 + 0 = 1

Input:n = 1000

Output:1 64 343 1000

**Approach:** For every perfect cube less than or equal to **n** keep on calculating the sum of its digits until the number is reduced to a single digit ( O(1) approach here ), if this digit is 1 then print the perfect cube else skip to the next perfect cube below **n** until all the perfect cubes have been considered.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <cmath> ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function that returns true if the eventual ` `// digit sum of number nm is 1 ` `bool` `isDigitSumOne(` `int` `nm) ` `{ ` ` ` `//if reminder will 1 ` ` ` `//then eventual sum is 1 ` ` ` `if` `(nm % 9 == 1) ` ` ` `return` `true` `; ` ` ` `else` ` ` `return` `false` `; ` `} ` ` ` `// Function to print the required numbers ` `// less than n ` `void` `printValidNums(` `int` `n) ` `{ ` ` ` `int` `cbrt_n = (` `int` `)cbrt(n); ` ` ` `for` `(` `int` `i = 1; i <= cbrt_n; i++) { ` ` ` `int` `cube = ` `pow` `(i, 3); ` ` ` ` ` `// If it is the required perfect cube ` ` ` `if` `(cube >= 1 && cube <= n && isDigitSumOne(cube)) ` ` ` `cout << cube << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 1000; ` ` ` `printValidNums(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function that returns true if the eventual ` ` ` `// digit sum of number nm is 1 ` ` ` `static` `boolean` `isDigitSumOne(` `int` `nm) ` ` ` `{ ` ` ` ` ` `//if reminder will 1 ` ` ` `//then eventual sum is 1 ` ` ` `if` `(nm % ` `9` `== ` `1` `) ` ` ` `return` `true` `; ` ` ` `else` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Function to print the required numbers ` ` ` `// less than n ` ` ` `static` `void` `printValidNums(` `int` `n) ` ` ` `{ ` ` ` `int` `cbrt_n = (` `int` `)Math.cbrt(n); ` ` ` `for` `(` `int` `i = ` `1` `; i <= cbrt_n; i++) { ` ` ` `int` `cube = (` `int` `)Math.pow(i, ` `3` `); ` ` ` ` ` `// If it is the required perfect cube ` ` ` `if` `(cube >= ` `1` `&& cube <= n && isDigitSumOne(cube)) ` ` ` `System.out.print(cube + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `1000` `; ` ` ` `printValidNums(n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the approach ` `import` `math ` ` ` `# Function that returns true if the eventual ` `# digit sum of number nm is 1 ` `def` `isDigitSumOne(nm) : ` ` ` `#if reminder will 1 ` ` ` `#then eventual sum is 1 ` ` ` `if` `(nm ` `%` `9` `=` `=` `1` `): ` ` ` `return` `True` ` ` `else` `: ` ` ` `return` `False` ` ` `# Function to print the required numbers ` `# less than n ` `def` `printValidNums(n): ` ` ` `cbrt_n ` `=` `math.ceil(n` `*` `*` `(` `1.` `/` `3.` `)) ` ` ` `for` `i ` `in` `range` `(` `1` `, cbrt_n ` `+` `1` `): ` ` ` `cube ` `=` `i ` `*` `i ` `*` `i ` ` ` `if` `(cube >` `=` `1` `and` `cube <` `=` `n ` `and` `isDigitSumOne(cube)): ` ` ` `print` `(cube, end ` `=` `" "` `) ` ` ` ` ` `# Driver code ` `n ` `=` `1000` `printValidNums(n) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns true if the ` ` ` `// eventual digit sum of number nm is 1 ` ` ` `static` `bool` `isDigitSumOne(` `int` `nm) ` ` ` `{ ` ` ` ` ` `//if reminder will 1 ` ` ` `//then eventual sum is 1 ` ` ` `if` `(nm % 9 == 1) ` ` ` `return` `true` `; ` ` ` `else` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Function to print the required ` ` ` `// numbers less than n ` ` ` `static` `void` `printValidNums(` `int` `n) ` ` ` `{ ` ` ` `int` `cbrt_n = (` `int` `)Math.Ceiling(Math.Pow(n, ` ` ` `(` `double` `) 1 / 3)); ` ` ` `for` `(` `int` `i = 1; i <= cbrt_n; i++) ` ` ` `{ ` ` ` `int` `cube = (` `int` `)Math.Pow(i, 3); ` ` ` ` ` `// If it is the required perfect cube ` ` ` `if` `(cube >= 1 && cube <= n && ` ` ` `isDigitSumOne(cube)) ` ` ` `Console.Write(cube + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 1000; ` ` ` `printValidNums(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by akt_mit ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function that returns true if the ` `// eventual digit sum of number nm is 1 ` `function` `isDigitSumOne(` `$nm` `) ` `{ ` ` ` `//if reminder will 1 ` ` ` `//then eventual sum is 1 ` ` ` `if` `(` `$nm` `% 9 == 1) ` ` ` `return` `true; ` ` ` `else` ` ` `return` `false; ` `} ` ` ` `// Function to print the required numbers ` `// less than n ` `function` `printValidNums(` `$n` `) ` `{ ` ` ` `$cbrt_n` `= ` `ceil` `(pow(` `$n` `,1/3)); ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$cbrt_n` `; ` `$i` `++) ` ` ` `{ ` ` ` `$cube` `= pow(` `$i` `, 3); ` ` ` ` ` `// If it is the required perfect cube ` ` ` `if` `(` `$cube` `>= 1 && ` `$cube` `<= ` `$n` `&& ` ` ` `isDigitSumOne(` `$cube` `)) ` ` ` `echo` `$cube` `, ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `$n` `= 1000; ` `printValidNums(` `$n` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1 64 343 1000

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.

## Recommended Posts:

- Maximum of sum and product of digits until number is reduced to a single digit
- Smallest and Largest N-digit perfect cubes
- Finding sum of digits of a number until sum becomes single digit
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of Numbers such that difference between the number and sum of its digits not less than L
- Sum of all Perfect Cubes lying in the range [L, R] for Q queries
- Check if a number can be represented as sum of two consecutive perfect cubes
- Sum of Digits in a^n till a single digit
- Print n numbers such that their sum is a perfect square
- Count number less than N which are product of perfect squares
- Count numbers upto N which are both perfect square and perfect cube
- Print N numbers such that their product is a Perfect Cube
- Sum of cubes of first n even numbers
- Sum of cubes of first n odd natural numbers
- Largest number N which can be reduced to 0 in K steps
- Find K numbers with sum equal to N and sum of their squares maximized
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Previous perfect square and cube number smaller than number N
- Largest number M less than N such that XOR of M and N is even

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.