当前位置:网站首页>Computational hierarchy -- the problem of large numbers multiplying decimals
Computational hierarchy -- the problem of large numbers multiplying decimals
2022-06-12 13:24:00 【shlyyy】
C Language seeking 100 The class of --100!
Preface
Ideas :
1. The first thing to think about is how to store large integers in memory ? When the basic data type cannot be saved , First thought of using arrays to save .
2. Then we need to think about which form to save large numbers ?Binary、Decimal、Hexadecimal? Save a large number of Decimal The form is more in line with the habit , It is also convenient for the later multiplication of levels .
3. Then calculate the number of digits occupied by the decimal number of the result of the storage hierarchy , Used to dynamically request array memory . With the knowledge of encoder and decoder, it is easy to think of the result 10 The logarithm of the base can get the number of digits required by the decimal system .
Computational hierarchy :
The normal idea of multiplying two numbers , Such as 720*7,7 And 720 Multiply each bit starting with a bit of ,7 The result of multiplying each bit plus the carry of the previous multiplication , Divide 10 The remainder is the value of the standard , Divide 10 Rounding is to give the next carry . The result of each multiplication is saved in the array . Because it starts from a single bit and multiplies , Therefore, the array memory stores the low and high order data results from the low address to the high address , This is a bit like little endian.
100!
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> // malloc
#include <string.h> // memset
#include <math.h> // log10
#include <time.h> // clock
void Factorial(int nNumber);
int main()
{
int nNum = 0;
while (1)
{
printf("Please input the number:");
scanf("%d", &nNum);
Factorial(nNum);
}
return 0;
}
void Factorial(int nNumber)
{
int i = 0; // Multipliers for calculating hierarchy
int j = 0; // Index of each bit of the calculation result
int nTemp = 0; // Temporary product result
int nCarry = 0; // Save carry
int nLen = 0; // Save the required number of digits
int nMaxIndex = 0; // Save the highest index
char* chAry = NULL; // Pointer to the final calculated result
clock_t ckStart, ckStop;
double nBitCount = 0;
ckStart = clock(); // Start counting
// 1. The number of digits required for the calculation result
// nNumber The number of decimal digits occupied by the result of the hierarchy is saved in nLen in , Such as 5!=120, Occupy 3 position
for (i = 1; i <= nNumber; i++)
{
nBitCount = nBitCount + log10(i);
}
nLen = (int)ceil(nBitCount);
// 2. To apply for space
// Each digit stores 0-9, For everyone char Type data is enough to store
chAry = (char*)malloc(nLen + 1);
memset(chAry, 0, nLen + 1);
chAry[0] = 1;
// 3.Algorithm
// After the multiplication, the last carry is accumulated , Then calculate the remainder and carry
for (i = 1; i <= nNumber; i++) // i For from 1 The number of products to start
{
nCarry = 0;
for (j = 0; j <= nMaxIndex; j++)
{
nTemp = chAry[j] * i;
nTemp += nCarry; // Add the last carry
chAry[j] = nTemp % 10; // Save the remainder
nCarry = nTemp / 10; // Save carry for next cycle
if ((nMaxIndex == j) && (nCarry !=0)) // The highest bit product has carry
{
nMaxIndex++;
}
}
}
ckStop = clock();
// 4. Output results in reverse order
printf("Factorial(%d)=", nNumber);
for ((nLen == 0) ? (i = 0) : (i = nLen - 1); i >= 0; i--)
{
printf("%d", chAry[i]);
}
printf("\r\n");
printf("Takes %f ms\r\n", ((double)(ckStop - ckStart)));
// 5. Release space
if (chAry != NULL)
{
free(chAry);
chAry = NULL;
}
}
seckill 10000!
Every time 4 Position in one int Type array , Do the multiplication as a whole .
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> // malloc
#include <string.h> // memset
#include <math.h> // log10
#include <time.h> // clock
void Factorial(int nNumber);
int main()
{
int nNum = 0;
while (1)
{
printf("Please input the number:");
scanf("%d", &nNum);
Factorial(nNum);
}
return 0;
}
void Factorial(int nNumber)
{
int i = 0; // Multipliers for calculating hierarchy
int j = 0; // Index of each bit of the calculation result
int nTemp = 0; // Temporary product result
int nCarry = 0; // Save carry
int nLen = 0; // Save the required number of digits
int nMaxIndex = 0; // Save the highest index
int* nAry = NULL; // Pointer to the final calculated result
clock_t ckStart, ckStop;
double nBitCount = 0;
ckStart = clock(); // Start counting
// 1. The number of digits required for the calculation result
// nNumber The number of decimal digits occupied by the result of the hierarchy is saved in nLen in , Such as 5!=120, Occupy 3 position
for (i = 1; i <= nNumber; i++)
{
nBitCount = nBitCount + log10(i);
}
nLen = (int)ceil(nBitCount)/4;
// 2. To apply for space
nAry = (int*)malloc((nLen + 1)*sizeof(int));
memset(nAry, 0, (nLen + 1)*sizeof(int));
nAry[0] = 1;
// 3.Algorithm
// After the multiplication, the last carry is accumulated , Then calculate the remainder and carry
for (i = 1; i <= nNumber; i++) // i For from 1 The number of products to start
{
nCarry = 0;
for (j = 0; j <= nMaxIndex; j++)
{
nTemp = nAry[j] * i;
nTemp += nCarry; // Add the last carry
nAry[j] = nTemp % 10000; // Save the remainder
nCarry = nTemp / 10000; // Save carry for next cycle
if ((nMaxIndex == j) && (nCarry != 0)) // The highest bit product has carry
{
nMaxIndex++;
}
}
}
ckStop = clock();
// 4. Output results in reverse order
printf("Factorial(%d)=", nNumber);
printf("%d", (nLen == 0) ? 1 : nAry[nMaxIndex]);
for (i = nMaxIndex - 1; i >= 0; i--)
{
printf("%04d", nAry[i]);
}
printf("\r\n");
printf("Takes %f ms\r\n", ((double)(ckStop - ckStart)));
// 5. Release space
if (nAry != NULL)
{
free(nAry);
nAry = NULL;
}
}
边栏推荐
- 嵌入式驱动程序设计
- Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver
- A brief introduction to Verilog mode
- Summary of question brushing in leetcode sliding window
- [Title brushing] Super washing machine
- Dameng database DM8 Windows environment installation
- 章鱼网络进展月报 | 2022.5.1-5.31
- Unittest framework
- Wechat web developer tools tutorial, web development issues
- 构建嵌入式系统软件开发环境-建立交叉编译环境
猜你喜欢

Bitmap, bloom filter and hash sharding
![[embedded] serial communication and its case](/img/5c/2e691e5ef03c7d65fd514e8b940e7b.jpg)
[embedded] serial communication and its case

Automatic Generation of Visual-Textual Presentation Layout
![2061: [example 1.2] trapezoidal area](/img/83/79b73ca10615c852768aba8d2a5049.jpg)
2061: [example 1.2] trapezoidal area
![[wechat applet development] Part 1: development tool installation and program configuration](/img/a8/f4dcbde295ba7cf738d878464b3af0.png)
[wechat applet development] Part 1: development tool installation and program configuration

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

C#DBHelper_ FactoryDB_ GetConn

torch_ geometric message passing network

442 authors, 100 pages! It took Google 2 years to release the new benchmark big bench | open source

创新实训(十一)开发过程中的一些bug汇总
随机推荐
1001:Hello,World
Mui login database improvement and Ajax asynchronous processing [mui+flask+mongodb+hbuilderx]
leetcode 47. Permutations II full permutations II (medium)
Embedded system hardware composition - embedded system hardware architecture
There was an error installing mysql. Follow the link below to CMD
JSP jump problem, unable to display database data, and unable to jump
章鱼网络进展月报 | 2022.5.1-5.31
Wechat web developer tools tutorial, web development issues
深度学习的多个 loss 是如何平衡的?
Will the next star of PPT for workplace speech be you [perfect summary] at the moment
镜像扫描工具预研
A "murder case" caused by ES setting operation
Hudi key generation
How to solve the problem of data table query error when SQLite writes the registration function?
How to adapt the page size when iframe is embedded in a web page
hudi 键的生成(Key Generation)
Dameng database DM8 Windows environment installation
Unittest framework
[you code, I fix] whitesource was officially renamed mend
verilog-mode的简要介绍