📜  大写功率函数

📅  最后修改于: 2021-05-04 22:30:54             🧑  作者: Mango

我们给出了两个分别为基数和指数的数字x和n。编写一个函数来计算x ^ n,其中1 <= x,n <= 10000并可能发生溢出
例子:

Input : x = 5, n = 20
Output : 95367431640625

Input : x = 2, n = 100
Output : 1267650600228229401496703205376



在上面的示例中,2 ^ 100有31位数字,即使我们使用long long int可以存储最多18位数字,也无法存储这些数字。背后的想法是将x乘以n,然后将结果存储在res []数组中。
这是用于查找数字幂的算法。
功率(n)
1.创建最大大小的数组res []并将x存储在res []数组中,并将res_size初始化为x中的位数。
2.对从i = 2到n的所有数字进行跟随
…..x与res []相乘并更新res []和res_size以存储乘法结果。
乘(res [],x)
1.初始化进位为0。
2.对i = 0到res_size-1执行以下操作
…。一种。找到prod = res [i] * x + carry。
….b。将prod的最后一位存储在res [i]中,并将剩余的几位存储在进位中。
3.将进位的所有数字存储在res []中,并按位数增加res_size。

C++
// C++ program to compute
// factorial of big numbers
#include 
using namespace std;
 
// Maximum number of digits in
// output
#define MAX 100000
 
// This function multiplies x
// with the number represented by res[].
// res_size is size of res[] or
// number of digits in the number
// represented by res[]. This function
// uses simple school mathematics
// for multiplication.
// This function may value of res_size
// and returns the new value of res_size
int multiply(int x, int res[], int res_size) {
 
// Initialize carry
int carry = 0;
 
// One by one multiply n with
// individual digits of res[]
for (int i = 0; i < res_size; i++) {
    int prod = res[i] * x + carry;
 
    // Store last digit of
    // 'prod' in res[]
    res[i] = prod % 10;
 
    // Put rest in carry
    carry = prod / 10;
}
 
// Put carry in res and
// increase result size
while (carry) {
    res[res_size] = carry % 10;
    carry = carry / 10;
    res_size++;
}
return res_size;
}
 
// This function finds
// power of a number x
void power(int x, int n)
{
 
//printing value "1" for power = 0
if(n == 0 ){
    cout<<"1";
    return;
}
 
 
int res[MAX];
int res_size = 0;
int temp = x;
 
// Initialize result
while (temp != 0) {
    res[res_size++] = temp % 10;
    temp = temp / 10;
}
 
// Multiply x n times
// (x^n = x*x*x....n times)
for (int i = 2; i <= n; i++)
    res_size = multiply(x, res, res_size);
 
cout << x << "^" << n << " = ";
for (int i = res_size - 1; i >= 0; i--)
    cout << res[i];
}
 
// Driver program
int main() {
int exponent = 100;
int base = 20;
power(base, exponent);
return 0;
}


Java
// Java program to compute
// factorial of big numbers
class GFG {
// Maximum number of digits in
// output
static final int MAX = 100000;
 
// This function multiplies x
// with the number represented by res[].
// res_size is size of res[] or
// number of digits in the number
// represented by res[]. This function
// uses simple school mathematics
// for multiplication.
// This function may value of res_size
// and returns the new value of res_size
static int multiply(int x, int res[], int res_size) {
 
    // Initialize carry
    int carry = 0;
 
    // One by one multiply n with
    // individual digits of res[]
    for (int i = 0; i < res_size; i++) {
    int prod = res[i] * x + carry;
 
    // Store last digit of
    // 'prod' in res[]
    res[i] = prod % 10;
 
    // Put rest in carry
    carry = prod / 10;
    }
 
    // Put carry in res and
    // increase result size
    while (carry > 0) {
    res[res_size] = carry % 10;
    carry = carry / 10;
    res_size++;
    }
    return res_size;
}
 
// This function finds
// power of a number x
static void power(int x, int n) {
     
    //printing value "1" for power = 0
    if(n == 0 ){
    System.out.print("1");
    return;
}
    int res[] = new int[MAX];
    int res_size = 0;
    int temp = x;
 
    // Initialize result
    while (temp != 0) {
    res[res_size++] = temp % 10;
    temp = temp / 10;
    }
 
    // Multiply x n times
    // (x^n = x*x*x....n times)
    for (int i = 2; i <= n; i++)
    res_size = multiply(x, res, res_size);
 
    System.out.print(x + "^" + n + " = ");
    for (int i = res_size - 1; i >= 0; i--)
    System.out.print(res[i]);
}
// Driver code
public static void main(String[] args) {
    int exponent = 100;
    int base = 2;
    power(base, exponent);
}
}
// This code is contributed by Anant Agarwal.


Python3
# Python program to compute
# factorial of big numbers
 
# Maximum number of digits in
# output
MAX=100000
 
# This function multiplies x
# with the number represented by res[].
# res_size is size of res[] or
# number of digits in the number
# represented by res[]. This function
# uses simple school mathematics
# for multiplication.
# This function may value of res_size
# and returns the new value of res_size
def multiply(x, res, res_size):
 
    # Initialize carry
    carry = 0
 
    # One by one multiply n with
    # individual digits of res[]
    for i in range(res_size):
        prod = res[i] * x + carry
 
        # Store last digit of
        # 'prod' in res[]
        res[i] = prod % 10
 
        # Put rest in carry
        carry = prod // 10
 
    # Put carry in res and
    # increase result size
    while (carry):
        res[res_size] = carry % 10
        carry = carry // 10
        res_size+=1
 
    return res_size
 
 
# This function finds
# power of a number x
def power(x,n):
     
    # printing value "1" for power = 0
     if (n == 0) :
        print("1")
        return
     
    res=[0 for i in range(MAX)]
    res_size = 0
    temp = x
 
    # Initialize result
    while (temp != 0):
        res[res_size] = temp % 10;
        res_size+=1
        temp = temp // 10
 
 
    # Multiply x n times
    # (x^n = x*x*x....n times)
    for i in range(2, n + 1):
        res_size = multiply(x, res, res_size)
 
    print(x , "^" , n , " = ",end="")
    for i in range(res_size - 1, -1, -1):
        print(res[i], end="")
 
 
# Driver program
 
exponent = 100
base = 2
power(base, exponent)
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to compute
// factorial of big numbers
using System;
 
class GFG {
     
    // Maximum number of digits in
    // output
    static int MAX = 100000;
     
    // This function multiplies x
    // with the number represented by res[].
    // res_size is size of res[] or
    // number of digits in the number
    // represented by res[]. This function
    // uses simple school mathematics
    // for multiplication.
    // This function may value of res_size
    // and returns the new value of res_size
    static int multiply(int x, int []res,
                            int res_size)
    {
     
        // Initialize carry
        int carry = 0;
     
        // One by one multiply n with
        // individual digits of res[]
        for (int i = 0; i < res_size; i++)
        {
            int prod = res[i] * x + carry;
         
            // Store last digit of
            // 'prod' in res[]
            res[i] = prod % 10;
         
            // Put rest in carry
            carry = prod / 10;
        }
     
        // Put carry in res and
        // increase result size
        while (carry > 0)
        {
            res[res_size] = carry % 10;
            carry = carry / 10;
            res_size++;
        }
         
        return res_size;
    }
     
    // This function finds
    // power of a number x
    static void power(int x, int n)
    {
        //printing value "1" for power = 0
    if(n == 0 ){
    Console.Write("1");
    return;
    }
        int []res = new int[MAX];
        int res_size = 0;
        int temp = x;
     
        // Initialize result
        while (temp != 0) {
            res[res_size++] = temp % 10;
            temp = temp / 10;
        }
     
        // Multiply x n times
        // (x^n = x*x*x....n times)
        for (int i = 2; i <= n; i++)
            res_size = multiply(x, res, res_size);
     
        Console.Write(x + "^" + n + " = ");
         
        for (int i = res_size - 1; i >= 0; i--)
            Console.Write(res[i]);
    }
     
    // Driver code
    public static void Main()
    {
        int exponent = 100;
        int b_ase = 2;
        power(b_ase, exponent);
    }
}
 
// This code is contributed by vt_m.


PHP
= 0; $i--, $O++)
if($res[$i])
break;
for ($i = count($res) - $O - 1;
            $i >= 0; $i--)
    echo $res[$i];
}
 
// Driver Code
$exponent = 100;
$base = 2;
power($base, $exponent);
 
// This code is contributed
// by mits
?>


输出:

2^100 = 1267650600228229401496703205376