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.
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:
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.
- Maximum of sum and product of digits until number is reduced to a single digit
- Smallest and Largest N-digit perfect cubes
- Number of perfect cubes between two given numbers
- Sum of Digits in a^n till a single digit
- Finding sum of digits of a number until sum becomes single digit
- Check whether a number can be expressed as a product of single digit numbers
- Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Count of n digit numbers whose sum of digits equals to given sum
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Print all n-digit numbers whose sum of digits equals to given sum
- N digit numbers divisible by 5 formed from the M digits
- Count of N-digit numbers with all distinct digits
- Count numbers formed by given two digit with sum having given digits
- Perfect cubes in a range
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- Count of N digit Numbers whose sum of every K consecutive digits is equal
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
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.