📜  浪费号码

📅  最后修改于: 2021-04-29 06:51:50             🧑  作者: Mango

许多N被称为浪费数。如果数字在N(包括权力)的因式分解的数量超过以N位数。所有素数都是浪费的。
例如,52是一个浪费数字,因为它的质数分解为2 * 2 * 13 ,并且其质数分解的总数为四位数(1、2、2、3),大于52中的位数。
前几个浪费数字是:

程序打印小于或等于给定数字N的浪费数字

给定数字N ,任务是打印小于或等于N的浪费数字
例子:

方法:

  1. 使用Sundaram筛网计算所有的质数,直到10 6
  2. 找出N中的位数。
  3. 找到N的所有素数,并对每个素数p执行以下操作:
    • 找出p中的位数。
    • 计算除以Np的最高幂。
    • 找到以上两个的总和。
  4. 如果素数中的位数大于原始数中的位数,则返回true。否则返回false。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
const int MAX = 10000;
 
// Array to store all prime less
// than and equal to MAX.
vector primes;
 
// Function for Sieve of Sundaram
void sieveSundaram()
{
    // Boolean Array
    bool marked[MAX / 2 + 1] = { 0 };
 
    // Mark all numbers which do not
    // generate prime number by 2*i+1
    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;
        }
    }
 
    // Since 2 is a prime number
    primes.push_back(2);
 
    // Print remaining primes are of the
    // form 2*i + 1 such that marked[i]
    // is false.
    for (int i = 1; i <= MAX / 2; i++)
        if (marked[i] == false)
            primes.push_back(2 * i + 1);
}
 
// Function that returns true if n
// is a Wasteful number
bool isWasteful(int n)
{
    if (n == 1)
        return false;
 
    // Count digits in original number
    int original_no = n;
    int sumDigits = 0;
 
    while (original_no > 0) {
        sumDigits++;
        original_no = original_no / 10;
    }
 
    int pDigit = 0, count_exp = 0, p;
 
    // Count all digits in prime factors of N
    // pDigit is going to hold this value.
    for (int i = 0; primes[i] <= n / 2; i++) {
 
        // Count powers of p in n
        while (n % primes[i] == 0) {
 
            // If primes[i] is a prime factor,
            p = primes[i];
            n = n / p;
 
            // Count the power of prime factors
            count_exp++;
        }
 
        // Add its digits to pDigit
        while (p > 0) {
            pDigit++;
            p = p / 10;
        }
 
        // Add digits of power of
        // prime factors to pDigit.
        while (count_exp > 1) {
            pDigit++;
            count_exp = count_exp / 10;
        }
    }
 
    // If n!=1 then one prime factor
    // still to be summed up
    if (n != 1) {
 
        while (n > 0) {
            pDigit++;
            n = n / 10;
        }
    }
 
    // If digits in prime factors is more than
    // digits in original number  then
    // return true. Else return false.
    return (pDigit > sumDigits);
}
 
// Function to print Wasteful Number before N
void Solve(int N)
{
    // Iterate till N and check if i
    // is wastefull or not
    for (int i = 1; i < N; i++) {
        if (isWasteful(i)) {
            cout << i << " ";
        }
    }
}
 
// Driver code
int main()
{
    // Precompute prime numbers upto 10^6
    sieveSundaram();
    int N = 10;
 
    // Function Call
    Solve(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
static int MAX = 10000;
 
// Array to store all prime less
// than and equal to MAX.
static Vector primes = new Vector();
 
// Function for Sieve of Sundaram
static void sieveSundaram()
{
    // Boolean Array
    boolean marked[] = new boolean[MAX / 2 + 1];
 
    // Mark all numbers which do not
    // generate prime number by 2*i+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;
        }
    }
 
    // Since 2 is a prime number
    primes.add(2);
 
    // Print remaining primes are of the
    // form 2*i + 1 such that marked[i]
    // is false.
    for (int i = 1; i <= MAX / 2; i++)
        if (marked[i] == false)
            primes.add(2 * i + 1);
}
 
// Function that returns true if n
// is a Wasteful number
static boolean isWasteful(int n)
{
    if (n == 1)
        return false;
 
    // Count digits in original number
    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;
 
    // Count all digits in prime factors of N
    // pDigit is going to hold this value.
    for (int i = 0; primes.get(i) <= n / 2; i++)
    {
 
        // Count powers of p in n
        while (n % primes.get(i) == 0)
        {
 
            // If primes[i] is a prime factor,
            p = primes.get(i);
            n = n / p;
 
            // Count the power of prime factors
            count_exp++;
        }
 
        // Add its digits to pDigit
        while (p > 0)
        {
            pDigit++;
            p = p / 10;
        }
 
        // Add digits of power of
        // prime factors to pDigit.
        while (count_exp > 1)
        {
            pDigit++;
            count_exp = count_exp / 10;
        }
    }
 
    // If n!=1 then one prime factor
    // still to be summed up
    if (n != 1)
    {
        while (n > 0)
        {
            pDigit++;
            n = n / 10;
        }
    }
 
    // If digits in prime factors is more than
    // digits in original number then
    // return true. Else return false.
    return (pDigit > sumDigits);
}
 
// Function to print Wasteful Number before N
static void Solve(int N)
{
    // Iterate till N and check if i
    // is wastefull or not
    for (int i = 1; i < N; i++)
    {
        if (isWasteful(i))
        {
            System.out.print(i + " ");
        }
    }
}
 
// Driver code
public static void main(String[] args)
{
    // Precompute prime numbers upto 10^6
    sieveSundaram();
    int N = 10;
 
    // Function Call
    Solve(N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the
# above approach
import math
MAX = 10000
 
# Array to store all prime
# less than and equal to MAX.
primes = []
 
# Function for Sieve of
# Sundaram
def sieveSundaram():
 
    # Boolean Array
    marked = [False] * ((MAX // 2) + 1)
 
    # Mark all numbers which do not
    # generate prime number by 2*i+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
 
    # Since 2 is a prime number
    primes.append(2)
 
    # Print remaining primes are of the
    # form 2*i + 1 such that marked[i]
    # is false.
    for i in range(1, (MAX // 2) + 1):
        if marked[i] == False:
            primes.append(2 * i + 1)
 
# Function that returns true if n
# is a Wasteful number
def isWasteful(n):
 
    if (n == 1):
        return False
 
    # Count digits in original
    # number
    original_no = n
    sumDigits = 0
 
    while (original_no > 0):
        sumDigits += 1
        original_no = original_no // 10
 
    pDigit, count_exp, p = 0, 0, 0
 
    # Count all digits in prime
    # factors of N pDigit is going
    # to hold this value.
    i = 0
     
    while(primes[i] <= (n//2)):
         
        # Count powers of p in n
        while (n % primes[i] == 0):
 
            # If primes[i] is a prime
            # factor,
            p = primes[i]
            n = n // p
 
            # Count the power of prime
            # factors
            count_exp += 1
 
        # Add its digits to pDigit
        while (p > 0):
            pDigit += 1
            p = p // 10
 
        # Add digits of power of
        # prime factors to pDigit.
        while (count_exp > 1):
            pDigit += 1
            count_exp = count_exp // 10
         
        i += 1
 
    # If n!=1 then one prime factor
    # still to be summed up
    if (n != 1):
        while (n > 0):
            pDigit += 1
            n = n // 10
 
    # If digits in prime factors
    # is more than digits in
    # original number then return
    # true. Else return false.
    return bool(pDigit > sumDigits)
 
# Function to print Wasteful Number
# before N
def Solve(N):
 
    # Iterate till N and check
    # if i is wastefull or not
    for i in range(1, N):
        if (isWasteful(i)):
            print(i, end = " ")
 
# Driver code
# Precompute prime numbers
# upto 10^6
sieveSundaram()
N = 10
 
# Function Call
Solve(N)
 
# This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
static int MAX = 10000;
 
// Array to store all prime less
// than and equal to MAX.
static List primes = new List();
 
// Function for Sieve of Sundaram
static void sieveSundaram()
{
    // Boolean Array
    bool []marked = new bool[MAX / 2 + 1];
 
    // Mark all numbers which do not
    // generate prime number by 2*i+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;
        }
    }
 
    // Since 2 is a prime number
    primes.Add(2);
 
    // Print remaining primes are of the
    // form 2*i + 1 such that marked[i]
    // is false.
    for (int i = 1; i <= MAX / 2; i++)
        if (marked[i] == false)
            primes.Add(2 * i + 1);
}
 
// Function that returns true if n
// is a Wasteful number
static bool isWasteful(int n)
{
    if (n == 1)
        return false;
 
    // Count digits in original number
    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;
 
    // Count all digits in prime factors of N
    // pDigit is going to hold this value.
    for (int i = 0; primes[i] <= n / 2; i++)
    {
 
        // Count powers of p in n
        while (n % primes[i] == 0)
        {
 
            // If primes[i] is a prime factor,
            p = primes[i];
            n = n / p;
 
            // Count the power of prime factors
            count_exp++;
        }
 
        // Add its digits to pDigit
        while (p > 0)
        {
            pDigit++;
            p = p / 10;
        }
 
        // Add digits of power of
        // prime factors to pDigit.
        while (count_exp > 1)
        {
            pDigit++;
            count_exp = count_exp / 10;
        }
    }
 
    // If n!=1 then one prime factor
    // still to be summed up
    if (n != 1)
    {
        while (n > 0)
        {
            pDigit++;
            n = n / 10;
        }
    }
 
    // If digits in prime factors is more than
    // digits in original number then
    // return true. Else return false.
    return (pDigit > sumDigits);
}
 
// Function to print Wasteful Number before N
static void Solve(int N)
{
    // Iterate till N and check if i
    // is wastefull or not
    for (int i = 1; i < N; i++)
    {
        if (isWasteful(i))
        {
            Console.Write(i + " ");
        }
    }
}
 
// Driver code
public static void Main(String[] args)
{
    // Precompute prime numbers upto 10^6
    sieveSundaram();
    int N = 10;
 
    // Function Call
    Solve(N);
}
}
 
// This code is contributed by 29AjayKumar


输出:
4 6 8 9

时间复杂度: O(N * log(log N))
参考: https : //oeis.org/A046760