Quantcast
Viewing all articles
Browse latest Browse all 4833

Raspberry Pi OS • Extracting the values of 'Perfect Number' 1 to 52

The recently discovered 'Perfect Number #52' has 82,048,640 digits from 388692.. to 008576. We can extract the value of any one of the fifty two 'Perfect Numbers' using Rust language .

Code:

ubu@raspberrypi:~ $ cargo new perfect_nr     Created binary (application) `perfect_nr` packageubu@raspberrypi:~ $ cd perfect_nrubu@raspberrypi:~/perfect_nr $ nano Cargo.tomlubu@raspberrypi:~/perfect_nr $ nano Cargo.tomlubu@raspberrypi:~/perfect_nr $ cd srcubu@raspberrypi:~/perfect_nr/src $ nano main.rsubu@raspberrypi:~/perfect_nr/src $ cd ..ubu@raspberrypi:~/perfect_nr $ cargo run    Updating crates.io index  Downloaded num-traits v0.2.19  Downloaded num-integer v0.1.46  Downloaded num-bigint v0.4.6  Downloaded autocfg v1.4.0  Downloaded 4 crates (194.5 KB) in 0.22s   Compiling autocfg v1.4.0   Compiling num-traits v0.2.19   Compiling num-integer v0.1.46   Compiling num-bigint v0.4.6   Compiling perfect_nr v0.1.0 (/home/ubu/perfect_nr)    Finished dev [unoptimized + debuginfo] target(s) in 5m 15s     Running `target/debug/perfect_nr`
How can the Raspberry Pi SBC be used to extract the value of any one of the 52 'Perfect Numbers'?


sudo -apt install rustc cargo

cargo new perfect_nr
cd perfect_nr
#edit Cargo.toml , add after [dependencies] ...
#use num_bigint::BigUint;
#use num_traits::One;
cd src
# replace main.rs
with the following rust code
#
cd ..
cargo run
------------------------------
use num_bigint::BigUint;
use num_traits::One;
use std::fs::File;
use std::io::{self, Write};

fn nth_perfect_number(n: usize) -> BigUint {
// Known exponents for the first 52 Mersenne primes (including the 52nd newly discovered)
let prime_exponents = [
2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127,
521, 607, 1279, 2203, 2281, 3217, 4253, 4423,
9689, 9941, 11213, 19937, 21701, 23209, 44497, 86243,
110503, 132049, 216091, 756839, 859433, 1257787,
1398269, 2976221, 3021377, 6972593, 13466917,
20996011, 24036583, 25964951, 30402457, 32582657,
37156667, 42643801, 43112609, 57885161, 74207281,
77232917, 82589933, 136279841 // 52nd exponent
];

if n == 0 || n > prime_exponents.len() {
panic!("Only up to the 52nd perfect number is supported");
}

let p = prime_exponents[n - 1];
let mersenne_prime = (BigUint::one() << p) - BigUint::one(); // 2^p - 1
let perfect_number = (BigUint::one() << (p - 1)) * &mersenne_prime; // 2^(p-1) * (2^p - 1)
perfect_number
}

fn main() -> io::Result<()> {
let n = 52; // Set to 52 for the newly discovered perfect number
let result = nth_perfect_number(n);

// Write the result to a text file in chunks for better readability
let mut file = File::create("perfect_number_52.txt")?;
let result_str = result.to_string();
let chunk_size = 1000; // Adjust this as necessary

for chunk in result_str.as_bytes().chunks(chunk_size) {
file.write_all(chunk)?;
file.write_all(b"\n")?; // New line after each chunk
}

println!("The 52nd perfect number has been written to perfect_number_52.txt in chunks.");
Ok(())
}

Statistics: Posted by geev03 — Sat Nov 16, 2024 9:12 am



Viewing all articles
Browse latest Browse all 4833

Trending Articles