当前位置:网站首页>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;
}
}
边栏推荐
- C language [23] classic interview questions [2]
- R language ggplot2 visualization: use the ggrep package to add a number label to the data point at the end of the line plot
- Multi source BFS problem template (with questions)
- JVM 运行时参数
- Summary of question brushing in leetcode sliding window
- 创新实训(十)高级界面美化
- 【刷题篇】超级洗衣机
- 1001:Hello,World
- 手把手教你IDEA创建SSM项目结构
- import torch_ Data view of geometric
猜你喜欢

403 you don't have permission to access this resource

torch_geometric mini batch 的那些事

微信web开发者工具使用教程,web开发问题

Bitmap, bloom filter and hash sharding
![Mui login database improvement and Ajax asynchronous processing [mui+flask+mongodb+hbuilderx]](/img/11/ce929f1cfbdcf245db9ee53bfe7a84.png)
Mui login database improvement and Ajax asynchronous processing [mui+flask+mongodb+hbuilderx]

Application of list and Dict

智能垃圾桶语音芯片应用设计方案介绍,WT588F02B-8S

成功跳槽阿里,进阶学习

【刷题篇】超级洗衣机
![[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
随机推荐
R language ggplot2 visualization: use the ggrep package to add a number label to the data point at the end of the line plot
TCP的“非”可靠性
Pre research of image scanning tool
Help you with everything from the basics to the source code. Introduce the technology in detail
2062: [example 1.3] movie tickets
单向环形链表实现约瑟夫环
2061: [example 1.2] trapezoidal area
B站分布式KV存储混沌工程实践
【云原生 | Kubernetes篇】深入了解Deployment(八)
【VIM】. Vimrc configuration, vundle and youcompleteme have been installed
Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
Experience and learning path of introductory deep learning and machine learning
Will the next star of PPT for workplace speech be you [perfect summary] at the moment
Octopus network progress monthly report | may 1-May 31, 2022
import torch_geometric 第一个图网络例子
实战 | 巧用位姿解算实现单目相机测距
STM32F1与STM32CubeIDE编程实例-设备驱动-DHT11温度温度传感器驱动
Mui login database improvement and Ajax asynchronous processing [mui+flask+mongodb+hbuilderx]
5V升压到12.6V的锂电池充电IC芯片方案FS4062B