-
-
Save rust-play/9bb0d9b0a435224c4011a989aedff4bf to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::f64; | |
use std::u8; | |
#[allow(dead_code)] | |
fn print_gnostr() { | |
let s = "gnostr"; | |
for byte in s.as_bytes() { | |
print!("{:02X} ", byte); | |
} | |
println!(); | |
for byte in s.as_bytes() { | |
print!("{} ", byte); | |
} | |
println!(); | |
} | |
fn main() { | |
//print_gnostr(); | |
let mut pi_mantissa: String = String::from(""); | |
let index_range: u32 = 197; // 197 // 1021 // 1990; // 1990 consistent on Code Runner | |
let index_start: u32 = 3; // 8000 // 3170 // 1018 //6567 ? | |
let index_end: u32 = index_start + index_range; | |
if index_end > 8197 {}; | |
for n in index_start..index_end { // Increased to 6001 to get 6000 digits. | |
pi_mantissa.push_str(&format!("{:X}", compute_spigot(n))); | |
//println!("{:0>30}:\n{}", n, pi_mantissa); | |
} | |
println!("pi_mantissa[(HEX)]:\n{}", pi_mantissa); | |
let pi_hex = pi_mantissa.as_str(); | |
let gnostr_hex = "AFED"; //"676E6F7374"; // Hex representation of "gnostr" | |
println!("Searching: {}", gnostr_hex); | |
if let Some(index) = pi_hex.find(gnostr_hex) { | |
println!("{} = pi_mantissa[{:}..{:}] ", gnostr_hex, index - 0, index + (gnostr_hex.len() - 1)); | |
//let decimal_index = (index - 0) / 1; // -2 for "3.", /1 because each hex char is one digit. | |
//println!("'{}' found after the {}th decimal place.", gnostr_hex, decimal_index - 0); | |
} else { | |
println!("'{}' not found in pi hex string.", gnostr_hex); | |
// let gnostr_xor = gnostr_hex.clone(); | |
// println!("{}",gnostr_xor); | |
} | |
} | |
#[allow(dead_code)] | |
fn compute_pi_base10(precision: u16) -> f64 { | |
let mut pi: f64 = 0.0; | |
for ki32 in 0..precision + 1 { | |
let k: f64 = f64::from(ki32); | |
pi = pi | |
+ (f64::powf(16.0, k * -1.0) | |
* (4.0 / (8.0 * k + 1.0) | |
- 2.0 / (8.0 * k + 4.0) | |
- 1.0 / (8.0 * k + 5.0) | |
- 1.0 / (8.0 * k + 6.0))); | |
} | |
pi | |
} | |
fn mod_pow(mut base: u32, mut exp: u32, modulus: u32) -> u32 { | |
if modulus == 1 { | |
return 0; | |
} | |
let mut result = 1; | |
base = base % modulus; | |
while exp > 0 { | |
if exp % 2 == 1 { | |
result = result * base % modulus; | |
} | |
exp = exp >> 1; | |
base = base * base % modulus | |
} | |
result | |
} | |
fn sigma(n: u32, j: u32) -> f64 { | |
let mut s: f64 = 0.0; | |
let mut denom = j; | |
for k in 0..n + 1 { | |
let r = mod_pow(16, n - k, denom); | |
s = fract_mod(s + f64::from(r) / f64::from(denom)); | |
denom += 8u32; | |
} | |
let mut num = 1.0 / 16.0; | |
while num / f64::from(denom) > f64::EPSILON { | |
s += num / f64::from(denom); | |
num /= 16.0; | |
denom += 8u32; | |
} | |
s.fract() | |
} | |
fn fract_mod(v: f64) -> f64 { | |
if v < 0.0 { | |
return 1.0 + v.fract() % 1.0; | |
} | |
v.fract() % 1.0 | |
} | |
fn f64_to_u8(value: f64) -> u8 { | |
let clamped_value = value.round().max(u8::MIN as f64).min(u8::MAX as f64); | |
clamped_value as u8 | |
} | |
fn compute_spigot(digit: u32) -> u8 { | |
let r = fract_mod( | |
4.0 * sigma(digit - 1, 1) | |
- (2.0 * sigma(digit - 1, 4)) | |
- sigma(digit - 1, 5) | |
- sigma(digit - 1, 6), | |
); | |
f64_to_u8((16.0 * r).floor()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment