How to resolve the algorithm Primorial numbers step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Primorial numbers step by step in the Rust programming language
Table of Contents
Problem Statement
Primorial numbers are those formed by multiplying successive prime numbers.
The primorial number series is: To express this mathematically, primorialn is the product of the first n (successive) primes:
In some sense, generating primorial numbers is similar to factorials. As with factorials, primorial numbers get large quickly.
By length (above), it is meant the number of decimal digits in the numbers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Primorial numbers step by step in the Rust programming language
Source code in the rust programming language
extern crate primal;
extern crate rayon;
extern crate rug;
use rayon::prelude::*;
use rug::Integer;
fn partial(p1 : usize, p2 : usize) -> String {
let mut aux = Integer::from(1);
let (_, hi) = primal::estimate_nth_prime(p2 as u64);
let sieve = primal::Sieve::new(hi as usize);
let prime1 = sieve.nth_prime(p1);
let prime2 = sieve.nth_prime(p2);
for i in sieve.primes_from(prime1).take_while(|i| *i <= prime2) {
aux = Integer::from(aux * i as u32);
}
aux.to_string_radix(10)
}
fn main() {
let mut j1 = Integer::new();
for k in [2,3,5,7,11,13,17,19,23,29].iter() {
j1.assign_primorial(*k);
println!("Primorial : {}", j1);
}
println!("Digits of primorial 10 : {}", partial(1, 10).chars().fold(0, |n, _| n + 1));
println!("Digits of primorial 100 : {}", partial(1, 100).chars().fold(0, |n, _| n + 1));
println!("Digits of primorial 1_000 : {}", partial(1, 1_000).chars().fold(0, |n, _| n + 1));
println!("Digits of primorial 10_000 : {}", partial(1, 10_000).chars().fold(0, |n, _| n + 1));
println!("Digits of primorial 100_000 : {}", partial(1, 100_000).chars().fold(0, |n, _| n + 1));
let mut auxi = Integer::from(1);
let ranges = vec![[1, 300_000], [300_001, 550_000], [550_001, 800_000], [800_001, 1_000_000]];
let v = ranges.par_iter().map(|value| partial(value[0], value[1])).collect::<Vec<_>>();
for i in v.iter() {
auxi =Integer::from(&auxi * i.parse::<Integer>().unwrap());
}
let result = auxi.to_string_radix(10).chars().fold(0, |n, _| n+1);
println!("Digits of primorial 1_000_000 : {}",result);
}
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Perl programming language
You may also check:How to resolve the algorithm Sequence: nth number with exactly n divisors step by step in the D programming language
You may also check:How to resolve the algorithm Factorial step by step in the XLISP programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Batch File programming language
You may also check:How to resolve the algorithm Forest fire step by step in the Ol programming language