当前位置:网站首页>[rust weekly database] num bigint - large integer
[rust weekly database] num bigint - large integer
2022-06-30 10:42:00 【51CTO】
We know rust The maximum number of integers supported in standard types is U128, So if you want to use an integer larger than this size ( It's mostly programming / Scientific operation / Blockchain ) What to do ? This is when we need our num-bigint
The library . Note that this library is included in num
This is in the yuan bank . We will introduce... For convenience num
library .
[dependencies]
num = "0.3"
If you want to use it alone num-bigint
Then you can.
[dependencies]
num-bigint = "0.3"
Basic usage
Let's take the calculation of Fibonacci sequence as an example :
use num::bigint::BigUint;
use num::traits::{Zero, One};
use std::mem::replace;
// The logic of calculating Fibonacci sequence
fn fib(n: usize) -> BigUint {
letmut f0: BigUint = Zero::zero();
letmut f1: BigUint = One::one();
for _ in0..n {
let f2 = f0 + &f1; // Be careful &
// f0 <- f1, f1 <- f2
f0 = replace(&mut f1, f2);
}
f0
}
fn main() {
println!("fib(1000) = {}", fib(1000));
}
Random large numbers
Can also cooperate rand
Generating random large numbers . You need to use rand
Of feature, also rand
At present, you can only use 0.7 edition
[dependencies]
rand = "0.7"
num = { version = "0.3", features = ["rand"] }
Then you can generate random numbers
use num::bigint::{ToBigInt, RandBigInt};
fn main() {
// Randomly generate one bigint
letmut rng = rand::thread_rng();
let a = rng.gen_bigint(1000);
// Generate a specific range of bigint
let low = -10000.to_bigint().unwrap();
let high = 10000.to_bigint().unwrap();
let b = rng.gen_bigint_range(&low, &high);
println!("{}", a * b);
}
That's all num-bigint
The basic usage of .num
There are some other practical contents in the library that you can also explore .
边栏推荐
- Foster design method
- Chen Haotian won the national championship of the national finals of the 7th children's model star ceremony
- Node environment configuration
- 今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
- ArcGIS Pro脚本工具(5)——排序后删除重复项
- 我在鹅厂淘到了一波“炼丹神器”,开发者快打包
- Compétences Comb 27 @ Body sense Manipulator
- MySQL index, transaction and storage engine of database (1)
- 腾讯云数据库工程师能力认证重磅推出,各界共话人才培养难题
- The AOV function of R language was used for repeated measures ANOVA (one intra group factor and one inter group factor) and interaction Plot function and boxplot to visualize the interaction
猜你喜欢
CVPR 2022 | 清华&字节&京东提出BrT:用于视觉和点云3D目标检测的桥接Transformer
Google 辟谣放弃 TensorFlow,它还活着!
Eth is not connected to the ore pool
MySQL index, transaction and storage engine of database (2)
MySQL index, transaction and storage engine of database (1)
[email protected] control a dog's running on OLED"/>
Skill combing [email protected] control a dog's running on OLED
Robotframework learning notes: environment installation and robotframework browser plug-in installation
MySQL index, transaction and storage engine of database (3)
How to deploy deflationary combustion destruction contract code in BSC chain_ Deploy dividend and marketing wallet contract code
逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展
随机推荐
ArcGIS PRO + PS vectorized land use planning map
Migrate full RT thread to gd32f4xx (detailed)
技能梳理[email protected]+adxl345+电机震动+串口输出
GD32 RT-Thread OTA/Bootloader驱动函数
Didi open source agile test case management platform!
"Kunming City coffee map" was opened again, and coffee brought the city closer
CSDN博客运营团队2022年H1总结
Deployment of efficient and versatile clusters lvs+kept highly available clusters
Android 开发面试真题进阶版(附答案解析)
转卡通学习笔记
Gd32 RT thread PWM drive function
六月集训(第30天) —— 拓扑排序
技能梳理[email protected]在oled上控制一条狗的奔跑
Compare the maximum computing power of the Cenozoic top ant s19xp and the existing s19pro in bitland
Eth is not connected to the ore pool
TypeScript–es5中的类,继承,静态方法
Apple's 5g chip was revealed to have failed in research and development, and the QQ password bug caused heated discussion. Wei Lai responded to the short selling rumors. Today, more big news is here
Leetcode question brushing (I) -- double pointer (go Implementation)
Compétences Comb 27 @ Body sense Manipulator
潘多拉 IOT 开发板学习(HAL 库)—— 实验1 跑马灯(RGB)实验(学习笔记)