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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Highly Composite Numbers
- Count of Numbers in Range where first digit is equal to last digit of the number
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Split n into maximum composite numbers
- Product of all the Composite Numbers in an array
- Represent the given number as the sum of two composite numbers
- Find two Composite Numbers such that there difference is N
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Sum of all composite numbers lying in the range [L, R] for Q queries
- Find a range of composite numbers of given length
- Sum and Product of all Composite numbers which are divisible by k in an array
- Sum and product of k smallest and k largest composite numbers in the array
- Find a sequence of N prime numbers whose sum is a composite number
- Generate a list of n consecutive composite numbers (An interesting method)
- Queries for the difference between the count of composite and prime numbers in a given range
- Count n digit numbers not having a particular digit
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1
- Count numbers from 1 to n that have 4 as a digit
- Largest Even and Odd N-digit numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.