1 text format
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
/// <summary>
/// The four arithmetic operations of large numbers (addition, subtraction, multiplication and division) and factorial operation
/// The multiplication calculation includes the primary school student algorithm, Karatsuba and Toom-Cook3 algorithm
/// The division operation is Truffer's own algorithm;
// / </summary>
public static class BigInteger_Utility
{ /// <summary> /// Record the number of addition, subtraction, multiplication and division operations /// </summary> public static int[] operations { get; set; } = new int[] { 0, 0, 0, 0 };
/// <summary>
/// Lesson 19 of "Xiaobai Programming": Random Number (Random) Sixth, randomly generate large numbers of any length (BigInteger)
/// Generally, numbers exceeding 9 digits can be Become a "big number".
/// The four arithmetic operations between two large numbers are used in cryptography, high-precision calculations and other applications.
/// Floating point numbers with many digits can be converted into large numbers and then reversed.
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public static string rand(int n)
{ // Random number generator Random rnd = new Random(); StringBuilder sb = new StringBuilder(); // The first number cannot be 0, so: a random number between 0-8 + 1 = 1-9 sb.Append((rnd.Next(9) + 1).ToString()); // The next n-1 numbers are 0-9; counting starts from 1 for (int i = 1; i < n; i++) {
sb.Append((rnd.Next(10)).ToString());
}
return sb.ToString();
}
/// <summary>
/// Convert string numbers to arrays
/// Low digit (right) first, such as "123", n=6 is stored as 3,2,1,_,_,_
// / n may be greater than the length of a; the remaining positions are reserved for carry, etc.
/// </summary>
/// <param name="a"></param>
/// <param name="n">Maximum number of digits, followed by 0</param>
/// <returns> </returns>
public static int[] string_to_digitals(string a, int n)
{ // Convert string to "character array" char[] c = a.ToCharArray(); // Array to store numbers int[] d = new int[n]; // Starting from the rightmost (ones digit) number, transfer it to a digital array and participate in the subsequent calculation for (int i = a.Length - 1,
// The '0' character is the smallest numeric character
// value = character - '0' ;
d[j++] = a[i] - '0';
}
return d;
}
/// <summary>
/// Convert array number to string type
/// Low digit (right) first, such as 3,2,1,_,_,_ converted to "123", n=6
// / This is the reverse calculation function of the previous string_to_digitals
/// n may be greater than the length of d; the remaining positions are reserved for carry, etc.
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
public static string digitals_to_string(int[] d)
{ int n = d.
StringBuilder sb = new StringBuilder();
for (; k >= 0; k--) sb.Append(d[k]);
return sb.ToString();
}
else
{
return "0";
}
}
}
2 code format
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
/// <summary>
/// 大数的(加减乘除)四则运算、阶乘运算
/// 乘法计算包括小学生算法、Karatsuba和Toom-Cook3算法
/// 除法运算为 Truffer 自创算法;
/// </summary>
public static class BigInteger_Utility
{
/// <summary>
/// 记录 加减乘除 的运算次数
/// </summary>
public static int[] operations { get; set; } = new int[] { 0, 0, 0, 0 };
/// <summary>
/// 《小白学程序》第十九课:随机数(Random)第六,随机生成任意长度的大数(BigInteger)
/// 一般可将超过9位数的数字成为“大数”。
/// 两个大数之间的四则运算用于密码学、高精度计算等应用。
/// 位数很多的浮点数可转为大数,再逆转即可。
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public static string rand(int n)
{
// 随机数发生器
Random rnd = new Random();
StringBuilder sb = new StringBuilder();
// 第一个数字不能为0,故:0-8之间的随机数+ 1 = 1-9
sb.Append((rnd.Next(9) + 1).ToString());
// 后面 n-1 个数字为 0-9;从 1 开始计数
for (int i = 1; i < n; i++)
{
sb.Append((rnd.Next(10)).ToString());
}
return sb.ToString();
}
/// <summary>
/// 字符串型的数字转为数组
/// 低位(右)在前,比如 "123" , n=6 存为 3,2,1,_,_,_
/// n 可能大于 a 的长度;剩余位置留出来用于 进位 等。
/// </summary>
/// <param name="a"></param>
/// <param name="n">最大位数,后面留0</param>
/// <returns></returns>
public static int[] string_to_digitals(string a, int n)
{
// 字符串 转为 “字符数组”
char[] c = a.ToCharArray();
// 存储数字的数组
int[] d = new int[n];
// 从最右端(个位)数字开始,转存为数字数组,参与后面的计算
for (int i = a.Length - 1, j = 0; i >= 0; i--)
{
// 跳过数字前面可能有的 - 号
if (a[i] == '-') continue;
// '0' 字符是最小的数字字符
// 数值 = 字符 - '0' ;
d[j++] = a[i] - '0';
}
return d;
}
/// <summary>
/// 数组型数字转为字符串型
/// 低位(右)在前,比如 3,2,1,_,_,_ 转为 "123", n=6
/// 这是前面 string_to_digitals 的反向计算函数
/// n 可能大于 d 的长度;剩余位置留出来用于 进位 等。
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
public static string digitals_to_string(int[] d)
{
int n = d.Length;
// 数字数组 d 含有一些无效的数组;
// 因此,先从最右段开始去除无效的位置
int k = n - 1;
//for (; (k >= 0) && (d[k] == 0); k--) ;
while ((k >= 0) && (d[k] == 0)) k--;
// 找到有效位置后,开始组合字符串;
if (k >= 0)
{
StringBuilder sb = new StringBuilder();
for (; k >= 0; k--) sb.Append(d[k]);
return sb.ToString();
}
else
{
return "0";
}
}
}