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

11 May, 2021
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++
Java
Python
C#
PHP
Javascript
// 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 1bool 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 nvoid 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 codeint main(){    int n = 1000;    printValidNums(n);    return 0;}
// Java implementation of the approachclass 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);    }}
# Python3 implementation of the approachimport math # Function that returns true if the eventual# digit sum of number nm is 1def 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 ndef 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 coden = 1000printValidNums(n)
// C# implementation of the approachusing 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
<?php// PHP implementation of the approach // Function that returns true if the// eventual digit sum of number nm is 1function 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 nfunction 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?>
<script>    // Javascript 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)    {        let cbrt_n = Math.ceil(Math.pow(n, 1 / 3));        for (let i = 1; i <= cbrt_n; i++)        {            let cube = Math.pow(i, 3);               // If it is the required perfect cube            if (cube >= 1 && cube <= n && isDigitSumOne(cube))                document.write(cube + " ");        }    }         let n = 1000;      printValidNums(n);     </script>
1 64 343 1000
 
jit_t
ankthon
Chandan_Agrawal
Akanksha_Rai
suresh07
maths-perfect-cube
number-theory
Mathematical
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Count ways to reach the n'th stair
Add two numbers without using arithmetic operators
Program to add two binary strings
Program to convert a given number to words
Modular multiplicative inverse