# Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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, 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 ` `#include ` `using` `namespace` `std; ` ` `  `// Functin that returns true if the eventual ` `// digit sum of number nm is 1 ` `bool` `isDigitSumOne(``int` `nm) ` `{ ` ` `  `    ``// Loop till the sum is not single digit number ` `    ``while` `(nm > 9) { ` ` `  `        ``// Intitialize the sum as zero ` `        ``int` `sum_digit = 0; ` ` `  `        ``// Find the sum of digits ` `        ``while` `(nm > 0) { ` `            ``int` `digit = nm % 10; ` `            ``sum_digit = sum_digit + digit; ` `            ``nm = nm / 10; ` `        ``} ` `        ``nm = sum_digit; ` `    ``} ` ` `  `    ``// If sum is eventually 1 ` `    ``if` `(nm == 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; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG { ` ` `  `    ``// Functin that returns true if the eventual ` `    ``// digit sum of number nm is 1 ` `    ``static` `boolean` `isDigitSumOne(``int` `nm) ` `    ``{ ` ` `  `        ``// Loop till the sum is not single digit number ` `        ``while` `(nm > ``9``) { ` ` `  `            ``// Intitialize the sum as zero ` `            ``int` `sum_digit = ``0``; ` ` `  `            ``// Find the sum of digits ` `            ``while` `(nm > ``0``) { ` `                ``int` `digit = nm % ``10``; ` `                ``sum_digit = sum_digit + digit; ` `                ``nm = nm / ``10``; ` `            ``} ` `            ``nm = sum_digit; ` `        ``} ` ` `  `        ``// If sum is eventually 1 ` `        ``if` `(nm == ``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); ` `    ``} ` `} `

## Python

 `# Python3 implementation of the approach ` `import` `math ` ` `  `# Functin that returns true if the eventual  ` `# digit sum of number nm is 1 ` `def` `isDigitSumOne(nm) : ` `     `  `    ``# Loop till the sum is not single digit number ` `    ``while``(nm>``9``) : ` `         `  `        ``# Intitialize the sum as zero ` `        ``sum_digit ``=` `0` `         `  `        ``# Find the sum of digits ` `        ``while``(nm !``=` `0``) : ` `            ``digit ``=` `nm ``%` `10` `            ``sum_digit ``=` `sum_digit ``+` `digit ` `            ``nm ``=` `nm ``/``/` `10` `        ``nm ``=` `sum_digit ` `     `  `    ``# If sum is eventually 1 ` `    ``if``(nm ``=``=` `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) `

## C#

// C# implementation of the approach
using System;

class GFG
{
// Functin that returns true if the
// eventual digit sum of number nm is 1
static bool isDigitSumOne(int nm)
{

// Loop till the sum is not single
// digit number
while (nm > 9)
{

// Intitialize the sum as zero
int sum_digit = 0;

// Find the sum of digits
while (nm > 0)
{
int digit = nm % 10;
sum_digit = sum_digit + digit;
nm = nm / 10;
}
nm = sum_digit;
}

// If sum is eventually 1
if (nm == 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 [tabbyending]

Output:

```1 64 343 1000
```

My Personal Notes arrow_drop_up

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.