📜  经济数字

📅  最后修改于: 2021-05-19 19:01:17             🧑  作者: Mango

经济号是一个数N如果在N(包括功率)的素因数分解数字的数量小于在N,N-位数。
前几个经济数字是:

查找小于N的经济数字

给定数字N ,任务是将所有经济数字打印到N。
例子:

方法:

  1. 使用Sundaram筛网计算所有质数,直至10 ^ 6。
  2. 找出N中的位数。
  3. 找到N的所有素数,并对每个素数P进行跟踪。
    • 找出P中的位数。
    • 计算除以N的P的最高幂。
    • 查找以上两个的总和。
  4. 如果素数中的位数小于原始数中的位数,则返回true。否则返回false。

下面是该方法的实现:

C++
// C++ implementation to find
// Economical Numbers till n
  
#include 
using namespace std;
const int MAX = 10000;
  
// Array to store all prime less
// than and equal to MAX.
vector primes;
  
// Utility function for sieve of sundaram
void sieveSundaram()
{
    // In general Sieve of Sundaram,
    // produces primes smaller
    // than (2*x + 2) for a number
    // given number x. Since
    // we want primes smaller than MAX,
    // we reduce MAX to half
    bool marked[MAX / 2 + 1] = { 0 };
  
    // Main logic of Sundaram. Mark all numbers which
    // do not generate prime number by doing 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 other primes. 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 to check if a number is
// a Economical number
bool isEconomical(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;
    }
  
    // Count all digits in prime factors of n
    // pDigit is going to hold this value.
    int pDigit = 0, count_exp = 0, p;
    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 less than
    // digits in original number  then
    // return true. Else return false.
    return (pDigit < sumDigits);
}
  
// Driver code
int main()
{
    // Finding all prime numbers
    // before limit. These
    // numbers are used to
    // find prime factors.
    sieveSundaram();
  
    for (int i = 1; i < 200; i++)
        if (isEconomical(i))
            cout << i << " ";
    return 0;
}


Java
// Java implementation to find
// Economical Numbers till n
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();
  
// Utility function for sieve of sundaram
static void sieveSundaram()
{
    // In general Sieve of Sundaram,
    // produces primes smaller
    // than (2*x + 2) for a number
    // given number x. Since
    // we want primes smaller than MAX,
    // we reduce MAX to half
    boolean []marked = new boolean[MAX / 2 + 1];
  
    // Main logic of Sundaram. Mark all numbers which
    // do not generate prime number by doing 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 other primes. 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 to check if a number is
// a Economical number
static boolean isEconomical(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;
    }
  
    // Count all digits in prime factors of n
    // pDigit is going to hold this value.
    int pDigit = 0, count_exp = 0, p = 0;
    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 less than
    // digits in original number  then
    // return true. Else return false.
    return (pDigit < sumDigits);
}
  
// Driver code
public static void main(String[] args)
{
    // Finding all prime numbers
    // before limit. These
    // numbers are used to
    // find prime factors.
    sieveSundaram();
  
    for (int i = 1; i < 200; i++)
        if (isEconomical(i))
            System.out.print(i + " ");
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation to find
# Economical Numbers till n
import math
MAX = 10000
  
# Array to store all prime less
# than and equal to MAX.
primes = []
  
# Utility function for sieve of sundaram
def sieveSundaram():
      
    # In general Sieve of Sundaram,
    # produces primes smaller
    # than (2*x + 2) for a number
    # given number x. Since
    # we want primes smaller than MAX,
    # we reduce MAX to half
    marked = [0] * (MAX // 2 + 1) 
      
    # Main logic of Sundaram. Mark all numbers which
    # do not generate prime number by doing 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)
      
    # Prother primes. 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 to check if a number is
# a Economical number
def isEconomical(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
      
    # Count all digits in prime factors of n
    # pDigit is going to hold this value.
    pDigit = 0
    count_exp = 0
    i = 0
    p = 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
        i += 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
      
    # 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 less than
    # digits in original number then
    # return true. Else return false.
    if (pDigit < sumDigits):
        return True
    return False
  
# Driver code
  
# Finding all prime numbers
# before limit. These
# numbers are used to
# find prime factors.
sieveSundaram()
  
for i in range(1, 200):
    if (isEconomical(i)):
        print(i, end = " ")
  
# This code is contributed by shubhamsingh10


C#
// C# implementation to find
// Economical Numbers till n
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();
  
// Utility function for sieve of sundaram
static void sieveSundaram()
{
      
    // In general Sieve of Sundaram,
    // produces primes smaller
    // than (2*x + 2) for a number
    // given number x. Since
    // we want primes smaller than MAX,
    // we reduce MAX to half
    bool[] marked = new bool[MAX / 2 + 1];
  
    // Main logic of Sundaram. Mark all numbers which
    // do not generate prime number by doing 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 other primes. 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 to check if a number is
// a Economical number
static bool isEconomical(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;
    }
  
    // Count all digits in prime factors of n
    // pDigit is going to hold this value.
    int pDigit = 0, count_exp = 0, p = 0;
    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 less than
    // digits in original number then
    // return true. Else return false.
    return (pDigit < sumDigits);
}
  
// Driver code
public static void Main(String[] args)
{
      
    // Finding all prime numbers
    // before limit. These
    // numbers are used to
    // find prime factors.
    sieveSundaram();
  
    for(int i = 1; i < 200; i++)
        if (isEconomical(i))
            Console.Write(i + " ");
}
}
  
// This code is contributed by Rajput-Ji


输出:
125 128

参考:https://mathworld.wolfram.com/EconomicalNumber.html