Wasteful Numbers
Last Updated :
20 Oct, 2021
A number N is called Wasteful Number if the number of digits in the prime factorization of N(including powers) is more than the number of digits in N. All prime numbers are Wasteful.
For Example, 52 is a Wasteful number as its prime factorization is 2*2*13, and its prime factorization has a total of four digits (1, 2, 2, 3) which is more than the total number of digits in 52.
First Few Wasteful Numbers are:
4, 6, 8, 9, 12, 18, 20, 22, 24,…
Program to print Wasteful Numbers less than or equals given number N
Given a number N, the task is to print Wasteful Numbers less than or equals to N.
Examples:
Input: N = 10
Output: 4, 6, 8, 9
Input: N = 12
Output: 4, 6, 8, 9, 12
Approach:
- Count all the prime numbers up to 106 using Sieve of Sundaram.
- Find the number of digits in N.
- Find all prime factors of N and do the following for every prime factor p:
- Find the number of digits in p.
- Count the highest power of p that divides N.
- Find the sum of the above two.
- If digits in prime factors are more than digits in the original number then return true. Else return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 10000;
vector< int > primes;
void sieveSundaram()
{
bool marked[MAX / 2 + 1] = { 0 };
for ( int i = 1;
i <= ( sqrt (MAX) - 1) / 2; i++) {
for ( int j = (i * (i + 1)) << 1;
j <= MAX / 2;
j = j + 2 * i + 1) {
marked[j] = true ;
}
}
primes.push_back(2);
for ( int i = 1; i <= MAX / 2; i++)
if (marked[i] == false )
primes.push_back(2 * i + 1);
}
bool isWasteful( int n)
{
if (n == 1)
return false ;
int original_no = n;
int sumDigits = 0;
while (original_no > 0) {
sumDigits++;
original_no = original_no / 10;
}
int pDigit = 0, count_exp = 0, p;
for ( int i = 0; primes[i] <= n / 2; i++) {
while (n % primes[i] == 0) {
p = primes[i];
n = n / p;
count_exp++;
}
while (p > 0) {
pDigit++;
p = p / 10;
}
while (count_exp > 1) {
pDigit++;
count_exp = count_exp / 10;
}
}
if (n != 1) {
while (n > 0) {
pDigit++;
n = n / 10;
}
}
return (pDigit > sumDigits);
}
void Solve( int N)
{
for ( int i = 1; i < N; i++) {
if (isWasteful(i)) {
cout << i << " " ;
}
}
}
int main()
{
sieveSundaram();
int N = 10;
Solve(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MAX = 10000 ;
static Vector<Integer> primes = new Vector<Integer>();
static void sieveSundaram()
{
boolean marked[] = new boolean [MAX / 2 + 1 ];
for ( int i = 1 ;
i <= (Math.sqrt(MAX) - 1 ) / 2 ; i++)
{
for ( int j = (i * (i + 1 )) << 1 ;
j <= MAX / 2 ;
j = j + 2 * i + 1 )
{
marked[j] = true ;
}
}
primes.add( 2 );
for ( int i = 1 ; i <= MAX / 2 ; i++)
if (marked[i] == false )
primes.add( 2 * i + 1 );
}
static boolean isWasteful( int n)
{
if (n == 1 )
return false ;
int original_no = n;
int sumDigits = 0 ;
while (original_no > 0 )
{
sumDigits++;
original_no = original_no / 10 ;
}
int pDigit = 0 , count_exp = 0 , p = 0 ;
for ( int i = 0 ; primes.get(i) <= n / 2 ; i++)
{
while (n % primes.get(i) == 0 )
{
p = primes.get(i);
n = n / p;
count_exp++;
}
while (p > 0 )
{
pDigit++;
p = p / 10 ;
}
while (count_exp > 1 )
{
pDigit++;
count_exp = count_exp / 10 ;
}
}
if (n != 1 )
{
while (n > 0 )
{
pDigit++;
n = n / 10 ;
}
}
return (pDigit > sumDigits);
}
static void Solve( int N)
{
for ( int i = 1 ; i < N; i++)
{
if (isWasteful(i))
{
System.out.print(i + " " );
}
}
}
public static void main(String[] args)
{
sieveSundaram();
int N = 10 ;
Solve(N);
}
}
|
Python3
import math
MAX = 10000
primes = []
def sieveSundaram():
marked = [ False ] * (( MAX / / 2 ) + 1 )
for i in range ( 1 , (( int (math.sqrt( MAX )) -
1 ) / / 2 ) + 1 ):
j = (i * (i + 1 )) << 1
while j < = ( MAX / / 2 ):
marked[j] = True
j = j + 2 * i + 1
primes.append( 2 )
for i in range ( 1 , ( MAX / / 2 ) + 1 ):
if marked[i] = = False :
primes.append( 2 * i + 1 )
def isWasteful(n):
if (n = = 1 ):
return False
original_no = n
sumDigits = 0
while (original_no > 0 ):
sumDigits + = 1
original_no = original_no / / 10
pDigit, count_exp, p = 0 , 0 , 0
i = 0
while (primes[i] < = (n / / 2 )):
while (n % primes[i] = = 0 ):
p = primes[i]
n = n / / p
count_exp + = 1
while (p > 0 ):
pDigit + = 1
p = p / / 10
while (count_exp > 1 ):
pDigit + = 1
count_exp = count_exp / / 10
i + = 1
if (n ! = 1 ):
while (n > 0 ):
pDigit + = 1
n = n / / 10
return bool (pDigit > sumDigits)
def Solve(N):
for i in range ( 1 , N):
if (isWasteful(i)):
print (i, end = " " )
sieveSundaram()
N = 10
Solve(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MAX = 10000;
static List< int > primes = new List< int >();
static void sieveSundaram()
{
bool []marked = new bool [MAX / 2 + 1];
for ( int i = 1;
i <= (Math.Sqrt(MAX) - 1) / 2; i++)
{
for ( int j = (i * (i + 1)) << 1;
j <= MAX / 2;
j = j + 2 * i + 1)
{
marked[j] = true ;
}
}
primes.Add(2);
for ( int i = 1; i <= MAX / 2; i++)
if (marked[i] == false )
primes.Add(2 * i + 1);
}
static bool isWasteful( int n)
{
if (n == 1)
return false ;
int original_no = n;
int sumDigits = 0;
while (original_no > 0)
{
sumDigits++;
original_no = original_no / 10;
}
int pDigit = 0, count_exp = 0, p = 0;
for ( int i = 0; primes[i] <= n / 2; i++)
{
while (n % primes[i] == 0)
{
p = primes[i];
n = n / p;
count_exp++;
}
while (p > 0)
{
pDigit++;
p = p / 10;
}
while (count_exp > 1)
{
pDigit++;
count_exp = count_exp / 10;
}
}
if (n != 1)
{
while (n > 0)
{
pDigit++;
n = n / 10;
}
}
return (pDigit > sumDigits);
}
static void Solve( int N)
{
for ( int i = 1; i < N; i++)
{
if (isWasteful(i))
{
Console.Write(i + " " );
}
}
}
public static void Main(String[] args)
{
sieveSundaram();
int N = 10;
Solve(N);
}
}
|
Javascript
<script>
let MAX = 10000;
let primes = [];
function sieveSundaram()
{
let marked = Array.from({length: MAX / 2 + 1},
(_, i) => 0);
for (let i = 1;
i <= Math.floor((Math.sqrt(MAX) - 1) / 2);
i++)
{
for (let j = (i * (i + 1)) << 1;
j <= Math.floor(MAX / 2);
j = j + 2 * i + 1)
{
marked[j] = true ;
}
}
primes.push(2);
for (let i = 1; i <= Math.floor(MAX / 2); i++)
if (marked[i] == false )
primes.push(2 * i + 1);
}
function isWasteful(n)
{
if (n == 1)
return false ;
let original_no = n;
let sumDigits = 0;
while (original_no > 0)
{
sumDigits++;
original_no = Math.floor(original_no / 10);
}
let pDigit = 0, count_exp = 0, p = 0;
for (let i = 0; primes[i] <= Math.floor(n / 2);
i++)
{
while (n % primes[i] == 0)
{
p = primes[i];
n = Math.floor(n / p);
count_exp++;
}
while (p > 0)
{
pDigit++;
p = Math.floor(p / 10);
}
while (count_exp > 1)
{
pDigit++;
count_exp = Math.floor(count_exp / 10);
}
}
if (n != 1)
{
while (n > 0)
{
pDigit++;
n = Math.floor(n / 10);
}
}
return (pDigit > sumDigits);
}
function Solve(N)
{
for (let i = 1; i < N; i++)
{
if (isWasteful(i))
{
document.write(i + " " );
}
}
}
sieveSundaram();
let N = 10;
Solve(N);
</script>
|
Time Complexity: O(N*log(log N))
Reference: https://oeis.org/A046760
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...