Composite numbers with digit sum 1
Last Updated :
23 Jun, 2022
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.
Examples:
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:
C++
#include <iostream>
using namespace std;
bool isComposite( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return false ;
if (n % 2 == 0 || n % 3 == 0)
return true ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return true ;
return false ;
}
bool isDigitSumOne( int nm)
{
while (nm > 9) {
int sum_digit = 0;
while (nm > 0) {
int digit = nm % 10;
sum_digit = sum_digit + digit;
nm = nm / 10;
}
nm = sum_digit;
}
if (nm == 1)
return true ;
else
return false ;
}
void printValidNums( int l, int r)
{
for ( int i = l; i <= r; i++) {
if (isComposite(i) && isDigitSumOne(i))
cout << i << " " ;
}
}
int main( void )
{
int l = 10, r = 100;
printValidNums(l, r);
return 0;
}
|
Java
public class GFG {
static boolean isComposite( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return false ;
if (n % 2 == 0 || n % 3 == 0 )
return true ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
if (n % i == 0 || n % (i + 2 ) == 0 )
return true ;
return false ;
}
static boolean isDigitSumOne( int nm)
{
while (nm > 9 ) {
int sum_digit = 0 ;
while (nm > 0 ) {
int digit = nm % 10 ;
sum_digit = sum_digit + digit;
nm = nm / 10 ;
}
nm = sum_digit;
}
if (nm == 1 )
return true ;
else
return false ;
}
static void printValidNums( int l, int r)
{
for ( int i = l; i <= r; i++) {
if (isComposite(i) && isDigitSumOne(i))
System.out.print(i + " " );
}
}
public static void main(String arg[])
{
int l = 10 , r = 100 ;
printValidNums(l, r);
}
}
|
Python3
def isComposite(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return False
if (n % 2 = = 0 or n % 3 = = 0 ):
return True
i = 5
while (i * i < = n):
if (n % i = = 0 or n % (i + 2 ) = = 0 ):
return True
i = i + 6
return False
def isDigitSumOne(nm) :
while (nm> 9 ) :
sum_digit = 0
while (nm ! = 0 ) :
digit = nm % 10
sum_digit = sum_digit + digit
nm = nm / / 10
nm = sum_digit
if (nm = = 1 ):
return True
else :
return False
def printValidNums(m, n ):
for i in range (m, n + 1 ):
if (isComposite(i) and isDigitSumOne(i)) :
print (i, end = " " )
l = 10
r = 100
printValidNums(m, n)
|
C#
using System;
class GFG
{
static bool isComposite( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return false ;
if (n % 2 == 0 || n % 3 == 0)
return true ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return true ;
return false ;
}
static bool isDigitSumOne( int nm)
{
while (nm > 9)
{
int sum_digit = 0;
while (nm > 0)
{
int digit = nm % 10;
sum_digit = sum_digit + digit;
nm = nm / 10;
}
nm = sum_digit;
}
if (nm == 1)
return true ;
else
return false ;
}
static void printValidNums( int l, int r)
{
for ( int i = l; i <= r; i++)
{
if (isComposite(i) && isDigitSumOne(i))
Console.Write(i + " " );
}
}
static public void Main ()
{
int l = 10, r = 100;
printValidNums(l, r);
}
}
|
PHP
<?php
function isComposite( $n )
{
if ( $n <= 1)
return false;
if ( $n <= 3)
return false;
if ( $n % 2 == 0 || $n % 3 == 0)
return true;
for ( $i = 5; $i * $i <= $n ; $i = $i + 6)
if ( $n % $i == 0 || $n % ( $i + 2) == 0)
return true;
return false;
}
function isDigitSumOne( $nm )
{
while ( $nm > 9)
{
$sum_digit = 0;
while ( $nm > 0)
{
$digit = $nm % 10;
$sum_digit = $sum_digit + $digit ;
$nm = floor ( $nm / 10);
}
$nm = $sum_digit ;
}
if ( $nm == 1)
return true;
else
return false;
}
function printValidNums( $l , $r )
{
for ( $i = $l ; $i <= $r ; $i ++)
{
if (isComposite( $i ) && isDigitSumOne( $i ))
echo $i , " " ;
}
}
$l = 10; $r = 100;
printValidNums( $l , $r );
?>
|
Javascript
<script>
function isComposite(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return false ;
if (n % 2 == 0 || n % 3 == 0)
return true ;
for (let i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return true ;
return false ;
}
function isDigitSumOne(nm)
{
while (nm > 9) {
let sum_digit = 0;
while (nm > 0) {
let digit = nm % 10;
sum_digit = sum_digit + digit;
nm = Math.floor(nm / 10);
}
nm = sum_digit;
}
if (nm == 1)
return true ;
else
return false ;
}
function printValidNums(l, r)
{
for (let i = l; i <= r; i++) {
if (isComposite(i) && isDigitSumOne(i))
document.write(i + " " );
}
}
let l = 10, r = 100;
printValidNums(l, r);
</script>
|
Output:
10 28 46 55 64 82 91 100
Time Complexity: O((r – l) * (sqrt(r – l) + log10(r – l)))
Auxiliary Space: O(1)
Optimizations : We can precompute composite numbers using Sieve Algorithms.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...