📜  数字的超分解

📅  最后修改于: 2021-05-04 07:19:37             🧑  作者: Mango

给定一个数字,任务是找到一个数字的超析因。

将给定数量的连续整数从1乘以给定数字(每个都提高到自己的幂)的结果称为数字的超析乘。

H(n)= 1 ^ 1 * 2 ^ 2 * 3 ^ 3 * . . . . . * n ^ n

例子:

一个幼稚的方法是两个使用两个循环,一个用于查找i ^ i的总和,另一个用于查找i ^ i。但是时间复杂度将是O(n 2 )。

一种有效的方法是使用内置的pow()函数或O(log n)方法查找i ^ i,然后将其添加。

下面是上述方法的实现。

C++
/// C++ program to find the hyperfactorial 
// of a number 
#include 
  
using namespace std;
#define ll long long 
  
// function to calculate the value of hyperfactorial
ll boost_hyperfactorial(ll num)
{
    // initialise the val to 1
    ll val = 1;
    for (int i = 1; i <= num; i++) {
        val = val * pow(i,i); 
    }
    // returns the hyperfactorial of a number
    return val;
}
  
// Driver code
int main()
{
    int num = 5;
    cout << boost_hyperfactorial(num);
    return 0;
}


Java
// Java program to find the 
// hyperfactorial of a number 
  
// function to calculate the 
// value of hyperfactorial
class GFG
{
static long boost_hyperfactorial(long num)
{
    // initialise the val to 1
    long val = 1;
    for (int i = 1; i <= num; i++) 
    {
        val = val * (long)Math.pow(i, i); 
    }
      
    // returns the hyperfactorial 
    // of a number
    return val;
}
  
// Driver code
public static void main(String args[])
{
    int num = 5;
    System.out.println(boost_hyperfactorial(num));
}
}
  
// This code is contributed 
// by chandan_jnu


Python3
# Python3 program to find the 
# hyperfactorial of a number 
  
# function to calculate the
# value of hyperfactorial
def boost_hyperfactorial(num):
  
    # initialise the 
    # val to 1
    val = 1;
    for i in range(1, num + 1):
        val = val * pow(i, i); 
      
    # returns the hyperfactorial
    # of a number
    return val;
  
# Driver code
num = 5;
print(boost_hyperfactorial(num));
  
# This code is contributed
# by mits


C#
// C# program to find the 
// hyperfactorial of a number 
using System;
  
class GFG
{
  
// function to calculate the 
// value of hyperfactorial
static long boost_hyperfactorial(long num)
{
    // initialise the val to 1
    long val = 1;
    for (long i = 1; i <= num; i++) 
    {
        val = val * (long)Math.Pow(i, i);
    }
      
    // returns the hyperfactorial 
    // of a number
    return val;
}
  
// Driver code
public static void Main()
{
    int num = 5;
    Console.WriteLine(boost_hyperfactorial(num));
}
}
  
// This code is contributed 
// by chandan_jnu


PHP


C++
// C++ program to find the hyperfactorial 
// of a number using boost libraries 
#include 
#include 
  
using namespace boost::multiprecision;
using namespace std;
  
// function to calculate the value of hyperfactorial
int1024_t boost_hyperfactorial(int num)
{
    // initialise the val to 1
    int1024_t val = 1;
    for (int i = 1; i <= num; i++) {
        for (int j = 1; j <= i; j++) {
            // 1^1*2^2*3^3....
            val *= i;
        }
    }
    // returns the hyperfactorial of a number
    return val;
}
  
// Driver code
int main()
{
    int num = 5;
    cout << boost_hyperfactorial(num);
    return 0;
}


Java
// Java program to find the hyperfactorial 
// of a number using boost libraries 
  
import java.io.*;
  
class GFG {
  
// function to calculate the value of hyperfactorial
static int boost_hyperfactorial(int num)
{
    // initialise the val to 1
    int val = 1;
    for (int i = 1; i <= num; i++) {
        for (int j = 1; j <= i; j++) {
            // 1^1*2^2*3^3....
            val *= i;
        }
    }
    // returns the hyperfactorial of a number
    return val;
}
  
// Driver code
  
  
    public static void main (String[] args) {
    int num = 5;
    System.out.println( boost_hyperfactorial(num));
    }
}
// This code is contributed 
// by chandan_jnu


Python3
# Python3 program to find the hyperfactorial 
# of a number using boost libraries 
  
# function to calculate the value of hyperfactorial 
def boost_hyperfactorial(num):
      
    # initialise the val to 1 
    val = 1; 
    for i in range(1,num+1): 
        for j in range(1,i+1): 
              
            # 1^1*2^2*3^3.... 
            val *= i; 
              
    # returns the hyperfactorial of a number 
    return val; 
  
# Driver code 
num = 5; 
print( boost_hyperfactorial(num)); 
  
# This code is contributed by mits


C#
// C# program to find the hyperfactorial 
// of a number using boost libraries 
using System;
  
class GFG 
{
  
// function to calculate the
// value of hyperfactorial
static int boost_hyperfactorial(int num)
{
    // initialise the val to 1
    int val = 1;
    for (int i = 1; i <= num; i++) 
    {
        for (int j = 1; j <= i; j++)
        {
            // 1^1*2^2*3^3....
            val *= i;
        }
    }
      
    // returns the hyperfactorial
    // of a number
    return val;
}
  
// Driver code
public static void Main ()
{
    int num = 5;
    Console.WriteLine(boost_hyperfactorial(num));
}
}
  
// This code is contributed 
// by chandan_jnu


PHP


输出:
86400000

时间复杂度: O(N * log N)

由于数字的超因子可能很大,因此数字将溢出。我们可以使用C++中的boost库或Java的BigInteger来存储数字N的超析因。

C++

// C++ program to find the hyperfactorial 
// of a number using boost libraries 
#include 
#include 
  
using namespace boost::multiprecision;
using namespace std;
  
// function to calculate the value of hyperfactorial
int1024_t boost_hyperfactorial(int num)
{
    // initialise the val to 1
    int1024_t val = 1;
    for (int i = 1; i <= num; i++) {
        for (int j = 1; j <= i; j++) {
            // 1^1*2^2*3^3....
            val *= i;
        }
    }
    // returns the hyperfactorial of a number
    return val;
}
  
// Driver code
int main()
{
    int num = 5;
    cout << boost_hyperfactorial(num);
    return 0;
}

Java

// Java program to find the hyperfactorial 
// of a number using boost libraries 
  
import java.io.*;
  
class GFG {
  
// function to calculate the value of hyperfactorial
static int boost_hyperfactorial(int num)
{
    // initialise the val to 1
    int val = 1;
    for (int i = 1; i <= num; i++) {
        for (int j = 1; j <= i; j++) {
            // 1^1*2^2*3^3....
            val *= i;
        }
    }
    // returns the hyperfactorial of a number
    return val;
}
  
// Driver code
  
  
    public static void main (String[] args) {
    int num = 5;
    System.out.println( boost_hyperfactorial(num));
    }
}
// This code is contributed 
// by chandan_jnu

Python3

# Python3 program to find the hyperfactorial 
# of a number using boost libraries 
  
# function to calculate the value of hyperfactorial 
def boost_hyperfactorial(num):
      
    # initialise the val to 1 
    val = 1; 
    for i in range(1,num+1): 
        for j in range(1,i+1): 
              
            # 1^1*2^2*3^3.... 
            val *= i; 
              
    # returns the hyperfactorial of a number 
    return val; 
  
# Driver code 
num = 5; 
print( boost_hyperfactorial(num)); 
  
# This code is contributed by mits

C#

// C# program to find the hyperfactorial 
// of a number using boost libraries 
using System;
  
class GFG 
{
  
// function to calculate the
// value of hyperfactorial
static int boost_hyperfactorial(int num)
{
    // initialise the val to 1
    int val = 1;
    for (int i = 1; i <= num; i++) 
    {
        for (int j = 1; j <= i; j++)
        {
            // 1^1*2^2*3^3....
            val *= i;
        }
    }
      
    // returns the hyperfactorial
    // of a number
    return val;
}
  
// Driver code
public static void Main ()
{
    int num = 5;
    Console.WriteLine(boost_hyperfactorial(num));
}
}
  
// This code is contributed 
// by chandan_jnu

的PHP


输出:
86400000