Given a range [L, R], the task is to find all the numbers from the range which are composite as well as the eventual sum of their digits is 1.
Input: L = 10, R = 100
Output: 10 28 46 55 64 82 91 100
10 = 1 + 0 = 1
28 = 2 + 8 = 10 = 1 + 0 = 1
91 = 9 + 1 = 10 = 1 + 0 = 1
100 = 1 + 0 + 0 = 1
Input: L = 250, R = 350
Output: 253 262 280 289 298 316 325 334 343
Approach: For every number in the range check if the number is composite i.e. it has a divisor other than 1 and the number itself. If the current number is a composite number then keep on calculating the sum of its digits until the number is reduced to a single digit, if this digit is 1 then the chosen number is a valid number.
Below is the implementation of the above approach:
10 28 46 55 64 82 91 100
Optimizations : We can precompute composite numbers using Sieve Algorithms.
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.
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Count of N-digit numbers having digit XOR as single digit
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Sum and Product of all Composite numbers which are divisible by k in an array
- Largest number less than N with digit sum greater than the digit sum of N
- Count n digit numbers not having a particular digit
- Count of Numbers in Range where first digit is equal to last digit of the number
- Product of all the Composite Numbers in an array
- Queries for the difference between the count of composite and prime numbers in a given range
- Smallest composite number not divisible by first N prime numbers
- Count and Sum of composite elements in an array
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count of n digit numbers whose sum of digits equals to given sum
- Print all n-digit numbers whose sum of digits equals to given sum
- Check if frequency of each digit is less than the digit
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Find the remainder when First digit of a number is divided by its Last digit
- Last digit of a number raised to last digit of N factorial
- Min steps to convert N-digit prime number into another by replacing a digit in each step
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.