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.
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- 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
- Split n into maximum composite numbers
- Represent the given number as the sum of two composite numbers
- Find two Composite Numbers such that there difference is N
- Find a range of composite numbers of given length
- Sum and Product of all Composite numbers which are divisible by k in an array
- Find a sequence of N prime numbers whose sum is a composite number
- Sum and product of k smallest and k largest composite numbers in the array
- 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 numbers having 0 as a digit
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.