📜  节俭数

📅  最后修改于: 2021-05-06 10:01:28             🧑  作者: Mango

节俭数字是指其位数严格大于其质数分解中的位数(包括指数)的数字。如果涉及素数因子分解的某个质数的指数为1,则该指数对素数因子分解中的位数不起作用。

节俭数字的一些示例是:

前几个节俭数字是:125、128、243、256、343、512、625、729等。

这里可能要注意,质数不是节俭数,因为质数的质数分解中的位数等于质数中的位数(因为未考虑值1的指数)。
示例19 = 19^1 ,但指数中的1不会影响数字的质数分解中的数字。因此,数字中的位数是两个(1和9),等于其素数分解中的位数(1和9)。

查找数字n是否节俭的程序涉及简单的步骤。首先,我们找到直到’n’的所有素数,然后找到n的素数分解。最后,我们检查n中的位数是否大于n的素数分解中的位数。

C++
// Program to check for Frugal number
#include 
using namespace std;
  
// Finding primes upto entered number
vector primes(long long int n)
{
    bool prime[n + 1];
  
    // Finding primes by Sieve of Eratosthenes method
    memset(prime, true, sizeof(prime));
    for (int i = 2; i * i <= n; i++) {
  
        // If prime[i] is not changed, then it is prime
        if (prime[i] == true) {
  
            // Update all multiples of p
            for (int j = i * 2; j <= n; j += i)
                prime[j] = false;
        }
    }
  
    // Forming array of the prime numbers found
    vector arr;    
    for (int i = 2; i < n; i++) 
        if (prime[i])
            arr.push_back(i);    
  
    return arr;
}
  
// Returns number of digits in n
int countDigits(long long int n)
{
    long long int temp = n;
    int c = 0;
    while (temp != 0) {
        temp = temp / 10;
        c++;
    }
    return c;
}
  
// Checking whether a number is Frugal or not
bool frugal(long long int n)
{
    vector r = primes(n);   
    long long int t = n;
    // Finding number of digits in prime  
    // factorization of the number
    long long int s = 0;
    for (int i = 0; i < r.size(); i++) {
        if (t % r[i] == 0) {
              
            // Exponent for current factor
            long long int k = 0;  
              
            // Counting number of times this prime 
            // factor divides (Finding exponent)
            while (t % r[i] == 0) {
                t = t / r[i];
                k++;
            }
  
            // Finding number of digits in the exponent    
            // Avoiding exponents of value 1
            if (k == 1) 
                s = s + countDigits(r[i]);
            else if (k != 1) 
                s = s + countDigits(r[i]) + countDigits(k);            
        }
    }
  
    // Checking condition for frugal number
    return (countDigits(n) > s && s != 0);
}
  
// Driver Method to check for frugal number
int main()
{
    long long int n = 343;
    if (frugal(n))
        cout << "A Frugal number\n";
    else
        cout << "Not a frugal number\n";
    return 0;
}


Java
// Program to check 
// for Frugal number
import java.io.*;
import java.util.*;
  
class GFG
{
    // Finding primes upto 
    // entered number
    static ArrayList 
           primes(long n)
    {
        boolean []prime = 
                new boolean[(int)n + 1];
        for(int i = 0; 
                i < n + 1; i++)
            prime[i] = true;
      
        // Finding primes by Sieve 
        // of Eratosthenes method
        for (int i = 2; 
                 i * i <= n; i++) 
        {
      
            // If prime[i] is not
            // changed, then it 
            // is prime
            if (prime[i] == true) 
            {
                // Update all
                // multiples of p
                for (int j = i * 2; 
                         j <= n; j += i)
                    prime[j] = false;
            }
        } 
          
        // Forming array of the 
        // prime numbers found
        ArrayList arr =
                 new ArrayList(); 
        for (int i = 2; i < n; i++) 
            if (prime[i])
                arr.add((long)i); 
  
        return arr;
    } 
      
    // Returns number
    // of digits in n
    static int countDigits(long n)
    {
        long temp = n;
        int c = 0;
        while (temp != 0) 
        {
            temp = temp / 10;
            c++;
        }
        return c;
    } 
      
    // Checking whether a 
    // number is Frugal or not
    static boolean frugal(long n)
    {
        ArrayList r = primes(n); 
        long t = n;
          
        // Finding number of digits 
        // in prime factorization 
        // of the number
        long s = 0;
        for (int i = 0; 
                 i < r.size(); i++) 
        {
            if (t % r.get(i) == 0) 
            {
                  
                // Exponent for 
                // current factor
                long k = 0; 
                  
                // Counting number of times 
                // this prime factor divides 
                // (Finding exponent)
                while (t % r.get(i) == 0) 
                {
                    t = t / r.get(i);
                    k++;
                }
      
                // Finding number of digits 
                // in the exponent Avoiding
                // exponents of value 1
                if (k == 1) 
                    s = s + countDigits(r.get(i));
                else if (k != 1) 
                    s = s + countDigits(r.get(i)) + 
                            countDigits(k);         
            }
        } 
          
        // Checking condition 
        // for frugal number
        return (countDigits(n) > s && s != 0);
    }
      
    // Driver Code
    public static void main(String[] args)
    {
        long n = 343;
        if (frugal(n))
            System.out.print("A Frugal number\n");
        else
            System.out.print("Not a frugal number\n");
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)


Python3
# Program to check for Frugal number
  
# Finding primes upto entered number
def primes(n):
  
    # Finding primes by Sieve
    # of Eratosthenes method
    prime = [True] * (n + 1);
      
    i = 2;
    while (i * i <= n):
          
        # If prime[i] is not changed,
        # then it is prime
        if (prime[i] == True):
              
            # Update all multiples of p
            j = i * 2;
            while (j <= n):
                prime[j] = False;
                j += i;
        i += 1;
      
    # Forming array of the prime
    # numbers found
    arr = []; 
    for i in range(2, n): 
        if (prime[i]):
            arr.append(i); 
  
    return arr;
  
# Returns number of digits in n
def countDigits(n):
  
    temp = n;
    c = 0;
    while (temp != 0):
        temp = int(temp / 10);
        c += 1;
    return c;
  
# Checking whether a number is
# Frugal or not
def frugal(n):
  
    r = primes(n); 
    t = n;
      
    # Finding number of digits 
    # in prime factorization 
    # of the number
    s = 0;
    for i in range(len(r)):
        if (t % r[i] == 0): 
              
            # Exponent for current factor
            k = 0; 
              
            # Counting number of times 
            # this prime factor divides 
            # (Finding exponent)
            while (t % r[i] == 0):
                t = int(t / r[i]);
                k += 1;
              
            # Finding number of digits
            # in the exponent Avoiding
            # exponents of value 1
            if (k == 1): 
                s = s + countDigits(r[i]);
            elif (k != 1): 
                s = (s + countDigits(r[i]) +
                         countDigits(k));     
      
    # Checking condition
    # for frugal number
    return (countDigits(n) > s and s != 0);
  
# Driver Code
n = 343;
if (frugal(n)):
    print("A Frugal number");
else:
    print("Not a frugal number");
      
# This code is contributed by 
# mits


C#
// Program to check for Frugal number
using System;
using System.Collections.Generic;
  
class GFG
{
    // Finding primes upto 
    // entered number
    static List primes(long n)
    {
        bool []prime = new bool[n + 1];
        for(int i = 0; i < n + 1; i++)
            prime[i] = true;
      
        // Finding primes by Sieve 
        // of Eratosthenes method
        for (int i = 2; i * i <= n; i++) 
        {
      
            // If prime[i] is not
            // changed, then it is prime
            if (prime[i] == true) 
            {
      
                // Update all multiples of p
                for (int j = i * 2; 
                         j <= n; j += i)
                    prime[j] = false;
            }
        } 
          
        // Forming array of the 
        // prime numbers found
        List arr = new List(); 
        for (int i = 2; i < n; i++) 
            if (prime[i])
                arr.Add(i); 
  
        return arr;
    } 
      
    // Returns number of digits in n
    static int countDigits(long n)
    {
        long temp = n;
        int c = 0;
        while (temp != 0) 
        {
            temp = temp / 10;
            c++;
        }
        return c;
    } 
      
    // Checking whether a number
    // is Frugal or not
    static bool frugal(long n)
    {
        List r = primes(n); 
        long t = n;
          
        // Finding number of digits in prime 
        // factorization of the number
        long s = 0;
        for (int i = 0; i < r.Count; i++) 
        {
            if (t % r[i] == 0) 
            {
                  
                // Exponent for current factor
                long k = 0; 
                  
                // Counting number of times 
                // this prime factor divides 
                // (Finding exponent)
                while (t % r[i] == 0) 
                {
                    t = t / r[i];
                    k++;
                }
      
                // Finding number of digits 
                // in the exponent Avoiding
                // exponents of value 1
                if (k == 1) 
                    s = s + countDigits(r[i]);
                else if (k != 1) 
                    s = s + countDigits(r[i]) + 
                            countDigits(k);         
            }
        } 
          
        // Checking condition 
        // for frugal number
        return (countDigits(n) > s && s != 0);
    }
      
    // Driver Code
    static void Main()
    {
        long n = 343;
        if (frugal(n))
            Console.Write("A Frugal number\n");
        else
            Console.Write("Not a frugal number\n");
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)


PHP
 $s &&
                        $s != 0);
} 
  
// Driver Code
$n = 343;
if (frugal($n))
    echo ("A Frugal number\n");
else
    echo ("Not a frugal number\n");
      
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>


输出:
A Frugal number

优化 :
上面的代码可以使用“打印所有主要因素及其功效”中讨论的方法进行优化。