给定一个数字n,我们需要计算该数字的阶乘除数之和。
例子:
Input : 4
Output : 60
Factorial of 4 is 24. Divisors of 24 are
1 2 3 4 6 8 12 24, sum of these is 60.
Input : 6
Output : 2418
一个简单的解决方案是首先计算给定数的阶乘,然后计算阶乘的数除数。该解决方案效率不高,并且可能会因阶乘计算而导致溢出。
下面是上述方法的实现:
C++
// C++ program to find sum of proper divisor of
// factorial of a number
#include
using namespace std;
// function to calculate factorial
int fact(int n)
{
if (n == 0)
return 1;
return n * fact(n - 1);
}
// function to calculate sum of divisor
int div(int x)
{
int ans = 0;
for (int i = 1; i<= x; i++)
if (x % i == 0)
ans += i;
return ans;
}
// Returns sum of divisors of n!
int sumFactDiv(int n)
{
return div(fact(n));
}
// Driver Code
int main()
{
int n = 4;
cout << sumFactDiv(n);
}
// This code is contributed
// by Akanksha Rai
C
// C program to find sum of proper divisor of
// factorial of a number
#include
// function to calculate factorial
int fact(int n) {
if (n == 0)
return 1;
return n * fact(n - 1);
}
// function to calculate sum of divisor
int div(int x) {
int ans = 0;
for (int i = 1; i<= x; i++)
if (x % i == 0)
ans += i;
return ans;
}
// Returns sum of divisors of n!
int sumFactDiv(int n) {
return div(fact(n));
}
// driver program
int main() {
int n = 4;
printf("%d",sumFactDiv(n));
}
Java
// Java program to find sum of proper divisor of
// factorial of a number
import java.io.*;
import java.util.*;
public class Division
{
// function to calculate factorial
static int fact(int n)
{
if (n == 0)
return 1;
return n*fact(n-1);
}
// function to calculate sum of divisor
static int div(int x)
{
int ans = 0;
for (int i = 1; i<= x; i++)
if (x%i == 0)
ans += i;
return ans;
}
// Returns sum of divisors of n!
static int sumFactDiv(int n)
{
return div(fact(n));
}
// driver program
public static void main(String args[])
{
int n = 4;
System.out.println(sumFactDiv(n));
}
}
Python3
# Python 3 program to find sum of proper
# divisor of factorial of a number
# function to calculate factorial
def fact(n):
if (n == 0):
return 1
return n * fact(n - 1)
# function to calculate sum
# of divisor
def div(x):
ans = 0;
for i in range(1, x + 1):
if (x % i == 0):
ans += i
return ans
# Returns sum of divisors of n!
def sumFactDiv(n):
return div(fact(n))
# Driver Code
n = 4
print(sumFactDiv(n))
# This code is contributed
# by Rajput-Ji
C#
// C# program to find sum of proper
// divisor of factorial of a number
using System;
class Division {
// function to calculate factorial
static int fac(int n)
{
if (n == 0)
return 1;
return n * fac(n - 1);
}
// function to calculate
// sum of divisor
static int div(int x)
{
int ans = 0;
for (int i = 1; i <= x; i++)
if (x % i == 0)
ans += i;
return ans;
}
// Returns sum of divisors of n!
static int sumFactDiv(int n)
{
return div(fac(n));
}
// Driver Code
public static void Main()
{
int n = 4;
Console.Write(sumFactDiv(n));
}
}
// This code is contributed by Nitin Mittal.
PHP
C++
// C++ program to find sum of divisors in n!
#include
#include
using namespace std;
// allPrimes[] stores all prime numbers less
// than or equal to n.
vector allPrimes;
// Fills above vector allPrimes[] for a given n
void sieve(int n)
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true. A value
// in prime[i] will finally be false if i is
// not a prime, else true.
vector prime(n+1, true);
// Loop to update prime[]
for (int p = 2; p*p <= n; p++)
{
// If prime[p] is not changed, then it
// is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p*2; i <= n; i += p)
prime[i] = false;
}
}
// Store primes in the vector allPrimes
for (int p = 2; p <= n; p++)
if (prime[p])
allPrimes.push_back(p);
}
// Function to find all result of factorial number
int factorialDivisors(int n)
{
sieve(n); // create sieve
// Initialize result
int result = 1;
// find exponents of all primes which divides n
// and less than n
for (int i = 0; i < allPrimes.size(); i++)
{
// Current divisor
int p = allPrimes[i];
// Find the highest power (stored in exp)'
// of allPrimes[i] that divides n using
// Legendre's formula.
int exp = 0;
while (p <= n)
{
exp = exp + (n/p);
p = p*allPrimes[i];
}
// Using the divisor function to calculate
// the sum
result = result*(pow(allPrimes[i], exp+1)-1)/
(allPrimes[i]-1);
}
// return total divisors
return result;
}
// Driver program to run the cases
int main()
{
cout << factorialDivisors(4);
return 0;
}
Java
// Java program to find sum of divisors in n!
import java.util.*;
class GFG{
// allPrimes[] stores all prime numbers less
// than or equal to n.
static ArrayList allPrimes=new ArrayList();
// Fills above vector allPrimes[] for a given n
static void sieve(int n)
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true. A value
// in prime[i] will finally be false if i is
// not a prime, else true.
boolean[] prime=new boolean[n+1];
// Loop to update prime[]
for (int p = 2; p*p <= n; p++)
{
// If prime[p] is not changed, then it
// is a prime
if (prime[p] == false)
{
// Update all multiples of p
for (int i = p*2; i <= n; i += p)
prime[i] = true;
}
}
// Store primes in the vector allPrimes
for (int p = 2; p <= n; p++)
if (prime[p]==false)
allPrimes.add(p);
}
// Function to find all result of factorial number
static int factorialDivisors(int n)
{
sieve(n); // create sieve
// Initialize result
int result = 1;
// find exponents of all primes which divides n
// and less than n
for (int i = 0; i < allPrimes.size(); i++)
{
// Current divisor
int p = allPrimes.get(i);
// Find the highest power (stored in exp)'
// of allPrimes[i] that divides n using
// Legendre's formula.
int exp = 0;
while (p <= n)
{
exp = exp + (n/p);
p = p*allPrimes.get(i);
}
// Using the divisor function to calculate
// the sum
result = result*((int)Math.pow(allPrimes.get(i), exp+1)-1)/
(allPrimes.get(i)-1);
}
// return total divisors
return result;
}
// Driver program to run the cases
public static void main(String[] args)
{
System.out.println(factorialDivisors(4));
}
}
// This code is contributed by mits
Python3
# Python3 program to find sum of divisors in n!
# allPrimes[] stores all prime numbers
# less than or equal to n.
allPrimes = [];
# Fills above vector allPrimes[]
# for a given n
def sieve(n):
# Create a boolean array "prime[0..n]"
# and initialize all entries it as true.
# A value in prime[i] will finally be
# false if i is not a prime, else true.
prime = [True] * (n + 1);
# Loop to update prime[]
p = 2;
while (p * p <= n):
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * 2, n + 1, p):
prime[i] = False;
p += 1;
# Store primes in the vector allPrimes
for p in range(2, n + 1):
if (prime[p]):
allPrimes.append(p);
# Function to find all result of factorial number
def factorialDivisors(n):
sieve(n); # create sieve
# Initialize result
result = 1;
# find exponents of all primes which
# divides n and less than n
for i in range(len(allPrimes)):
# Current divisor
p = allPrimes[i];
# Find the highest power (stored in exp)'
# of allPrimes[i] that divides n using
# Legendre's formula.
exp = 0;
while (p <= n):
exp = exp + int(n / p);
p = p * allPrimes[i];
# Using the divisor function to
# calculate the sum
result = int(result * (pow(allPrimes[i], exp + 1) - 1) /
(allPrimes[i] - 1));
# return total divisors
return result;
# Driver Code
print(factorialDivisors(4));
# This code is contributed by mits
C#
// C# program to find sum of divisors in n!
using System;
using System.Collections;
class GFG{
// allPrimes[] stores all prime numbers less
// than or equal to n.
static ArrayList allPrimes=new ArrayList();
// Fills above vector allPrimes[] for a given n
static void sieve(int n)
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true. A value
// in prime[i] will finally be false if i is
// not a prime, else true.
bool[] prime=new bool[n+1];
// Loop to update prime[]
for (int p = 2; p*p <= n; p++)
{
// If prime[p] is not changed, then it
// is a prime
if (prime[p] == false)
{
// Update all multiples of p
for (int i = p*2; i <= n; i += p)
prime[i] = true;
}
}
// Store primes in the vector allPrimes
for (int p = 2; p <= n; p++)
if (prime[p]==false)
allPrimes.Add(p);
}
// Function to find all result of factorial number
static int factorialDivisors(int n)
{
sieve(n); // create sieve
// Initialize result
int result = 1;
// find exponents of all primes which divides n
// and less than n
for (int i = 0; i < allPrimes.Count; i++)
{
// Current divisor
int p = (int)allPrimes[i];
// Find the highest power (stored in exp)'
// of allPrimes[i] that divides n using
// Legendre's formula.
int exp = 0;
while (p <= n)
{
exp = exp + (n/p);
p = p*(int)allPrimes[i];
}
// Using the divisor function to calculate
// the sum
result = result*((int)Math.Pow((int)allPrimes[i], exp+1)-1)/
((int)allPrimes[i]-1);
}
// return total divisors
return result;
}
// Driver program to run the cases
static void Main()
{
Console.WriteLine(factorialDivisors(4));
}
}
// This code is contributed by mits
PHP
输出 :
60
一个有效的解决方案基于Legendre的公式。以下是步骤。
- 查找所有小于或等于n(输入数字)的质数。我们可以为此使用筛网算法。令n为6。所有小于6的质数均为{2,3,5}。
- 对于每个素数p,找出最大的幂除以n!。为此,我们在勒让德的公式公式中使用以下公式。
除以p的最大乘方的值是每个项的下限值n / p + n /(p2)+ n /(p3)+……
令这些值为exp1,exp2,exp3,.。使用上面的公式,我们得到n = 6的下面的值。- 2的最大幂除以6!,exp1 = 4。
- 3的最大幂除以6!,exp2 = 2。
- 5的最大幂除以6 !, exp3 = 1。
- 结果基于除数函数。
C++
// C++ program to find sum of divisors in n! #include
#include using namespace std; // allPrimes[] stores all prime numbers less // than or equal to n. vector allPrimes; // Fills above vector allPrimes[] for a given n void sieve(int n) { // Create a boolean array "prime[0..n]" and // initialize all entries it as true. A value // in prime[i] will finally be false if i is // not a prime, else true. vector prime(n+1, true); // Loop to update prime[] for (int p = 2; p*p <= n; p++) { // If prime[p] is not changed, then it // is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p*2; i <= n; i += p) prime[i] = false; } } // Store primes in the vector allPrimes for (int p = 2; p <= n; p++) if (prime[p]) allPrimes.push_back(p); } // Function to find all result of factorial number int factorialDivisors(int n) { sieve(n); // create sieve // Initialize result int result = 1; // find exponents of all primes which divides n // and less than n for (int i = 0; i < allPrimes.size(); i++) { // Current divisor int p = allPrimes[i]; // Find the highest power (stored in exp)' // of allPrimes[i] that divides n using // Legendre's formula. int exp = 0; while (p <= n) { exp = exp + (n/p); p = p*allPrimes[i]; } // Using the divisor function to calculate // the sum result = result*(pow(allPrimes[i], exp+1)-1)/ (allPrimes[i]-1); } // return total divisors return result; } // Driver program to run the cases int main() { cout << factorialDivisors(4); return 0; } Java
// Java program to find sum of divisors in n! import java.util.*; class GFG{ // allPrimes[] stores all prime numbers less // than or equal to n. static ArrayList
allPrimes=new ArrayList (); // Fills above vector allPrimes[] for a given n static void sieve(int n) { // Create a boolean array "prime[0..n]" and // initialize all entries it as true. A value // in prime[i] will finally be false if i is // not a prime, else true. boolean[] prime=new boolean[n+1]; // Loop to update prime[] for (int p = 2; p*p <= n; p++) { // If prime[p] is not changed, then it // is a prime if (prime[p] == false) { // Update all multiples of p for (int i = p*2; i <= n; i += p) prime[i] = true; } } // Store primes in the vector allPrimes for (int p = 2; p <= n; p++) if (prime[p]==false) allPrimes.add(p); } // Function to find all result of factorial number static int factorialDivisors(int n) { sieve(n); // create sieve // Initialize result int result = 1; // find exponents of all primes which divides n // and less than n for (int i = 0; i < allPrimes.size(); i++) { // Current divisor int p = allPrimes.get(i); // Find the highest power (stored in exp)' // of allPrimes[i] that divides n using // Legendre's formula. int exp = 0; while (p <= n) { exp = exp + (n/p); p = p*allPrimes.get(i); } // Using the divisor function to calculate // the sum result = result*((int)Math.pow(allPrimes.get(i), exp+1)-1)/ (allPrimes.get(i)-1); } // return total divisors return result; } // Driver program to run the cases public static void main(String[] args) { System.out.println(factorialDivisors(4)); } } // This code is contributed by mits Python3
# Python3 program to find sum of divisors in n! # allPrimes[] stores all prime numbers # less than or equal to n. allPrimes = []; # Fills above vector allPrimes[] # for a given n def sieve(n): # Create a boolean array "prime[0..n]" # and initialize all entries it as true. # A value in prime[i] will finally be # false if i is not a prime, else true. prime = [True] * (n + 1); # Loop to update prime[] p = 2; while (p * p <= n): # If prime[p] is not changed, # then it is a prime if (prime[p] == True): # Update all multiples of p for i in range(p * 2, n + 1, p): prime[i] = False; p += 1; # Store primes in the vector allPrimes for p in range(2, n + 1): if (prime[p]): allPrimes.append(p); # Function to find all result of factorial number def factorialDivisors(n): sieve(n); # create sieve # Initialize result result = 1; # find exponents of all primes which # divides n and less than n for i in range(len(allPrimes)): # Current divisor p = allPrimes[i]; # Find the highest power (stored in exp)' # of allPrimes[i] that divides n using # Legendre's formula. exp = 0; while (p <= n): exp = exp + int(n / p); p = p * allPrimes[i]; # Using the divisor function to # calculate the sum result = int(result * (pow(allPrimes[i], exp + 1) - 1) / (allPrimes[i] - 1)); # return total divisors return result; # Driver Code print(factorialDivisors(4)); # This code is contributed by mits
C#
// C# program to find sum of divisors in n! using System; using System.Collections; class GFG{ // allPrimes[] stores all prime numbers less // than or equal to n. static ArrayList allPrimes=new ArrayList(); // Fills above vector allPrimes[] for a given n static void sieve(int n) { // Create a boolean array "prime[0..n]" and // initialize all entries it as true. A value // in prime[i] will finally be false if i is // not a prime, else true. bool[] prime=new bool[n+1]; // Loop to update prime[] for (int p = 2; p*p <= n; p++) { // If prime[p] is not changed, then it // is a prime if (prime[p] == false) { // Update all multiples of p for (int i = p*2; i <= n; i += p) prime[i] = true; } } // Store primes in the vector allPrimes for (int p = 2; p <= n; p++) if (prime[p]==false) allPrimes.Add(p); } // Function to find all result of factorial number static int factorialDivisors(int n) { sieve(n); // create sieve // Initialize result int result = 1; // find exponents of all primes which divides n // and less than n for (int i = 0; i < allPrimes.Count; i++) { // Current divisor int p = (int)allPrimes[i]; // Find the highest power (stored in exp)' // of allPrimes[i] that divides n using // Legendre's formula. int exp = 0; while (p <= n) { exp = exp + (n/p); p = p*(int)allPrimes[i]; } // Using the divisor function to calculate // the sum result = result*((int)Math.Pow((int)allPrimes[i], exp+1)-1)/ ((int)allPrimes[i]-1); } // return total divisors return result; } // Driver program to run the cases static void Main() { Console.WriteLine(factorialDivisors(4)); } } // This code is contributed by mits
的PHP
输出:
60