|
| 1 | +use std::io::Write as _; |
| 2 | + |
| 3 | +/// SeaORM Entity for state persistence |
| 4 | +mod state { |
| 5 | + use sea_orm::entity::prelude::*; |
| 6 | + use serde::{Deserialize, Serialize}; |
| 7 | + |
| 8 | + #[sea_orm::model] |
| 9 | + #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] |
| 10 | + #[sea_orm(table_name = "state")] |
| 11 | + pub struct Model { |
| 12 | + #[sea_orm(primary_key, auto_increment = false)] |
| 13 | + pub digits: u32, |
| 14 | + pub boxes: Digits, |
| 15 | + pub i: u32, |
| 16 | + pub nines: u32, |
| 17 | + pub predigit: u8, |
| 18 | + pub have_predigit: bool, |
| 19 | + pub count: u32, |
| 20 | + } |
| 21 | + |
| 22 | + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)] |
| 23 | + pub struct Digits(pub Vec<u32>); |
| 24 | + |
| 25 | + impl ActiveModelBehavior for ActiveModel {} |
| 26 | +} |
| 27 | + |
| 28 | +mod run_log { |
| 29 | + use sea_orm::entity::prelude::*; |
| 30 | + use serde::{Deserialize, Serialize}; |
| 31 | + |
| 32 | + #[sea_orm::model] |
| 33 | + #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] |
| 34 | + #[sea_orm(table_name = "run_log")] |
| 35 | + pub struct Model { |
| 36 | + #[sea_orm(primary_key)] |
| 37 | + pub id: i32, |
| 38 | + pub digits: u32, |
| 39 | + pub pi_digits: String, |
| 40 | + } |
| 41 | + |
| 42 | + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)] |
| 43 | + pub struct Digits(pub Vec<u32>); |
| 44 | + |
| 45 | + impl ActiveModelBehavior for ActiveModel {} |
| 46 | +} |
| 47 | + |
| 48 | +fn pi_spigot(digits: u32) { |
| 49 | + use sea_orm::{ActiveModelTrait, EntityTrait, IntoActiveModel, NotSet, Set, TransactionTrait}; |
| 50 | + |
| 51 | + // open database file, create if not exists |
| 52 | + let db = &sea_orm::Database::connect("sqlite://pi.sqlite").unwrap(); |
| 53 | + db.get_schema_builder() |
| 54 | + .register(state::Entity) |
| 55 | + .register(run_log::Entity) |
| 56 | + .sync(db) // create table if not exists |
| 57 | + .unwrap(); |
| 58 | + |
| 59 | + if digits == 0 { |
| 60 | + println!("3"); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + let len = digits as usize * 10 / 3 + 1; |
| 65 | + |
| 66 | + let state = match state::Entity::find_by_id(digits).one(db).unwrap() { |
| 67 | + Some(state) => { |
| 68 | + println!("resuming from {}th digit", state.count); |
| 69 | + state |
| 70 | + } |
| 71 | + None => state::Model { |
| 72 | + digits, |
| 73 | + boxes: state::Digits(vec![2u32; len]), |
| 74 | + i: 0, |
| 75 | + nines: 0, |
| 76 | + predigit: 0, |
| 77 | + have_predigit: false, |
| 78 | + count: 0, |
| 79 | + } |
| 80 | + .into_active_model() |
| 81 | + .insert(db) |
| 82 | + .unwrap(), |
| 83 | + }; |
| 84 | + let mut run_log = run_log::ActiveModel { |
| 85 | + id: NotSet, |
| 86 | + digits: Set(digits), |
| 87 | + pi_digits: Set("".to_owned()), |
| 88 | + } |
| 89 | + .save(db) |
| 90 | + .unwrap(); |
| 91 | + |
| 92 | + let mut boxes = state.boxes.0; |
| 93 | + let mut nines = state.nines; |
| 94 | + let mut predigit = state.predigit; |
| 95 | + let mut have_predigit = state.have_predigit; |
| 96 | + let mut count = state.count; |
| 97 | + let mut s = String::new(); |
| 98 | + |
| 99 | + for i in (0..(digits + 1)).skip(state.i as usize) { |
| 100 | + if count % 100 == 0 { |
| 101 | + std::io::stdout().flush().unwrap(); |
| 102 | + // save checkpoint |
| 103 | + let txn = db.begin().unwrap(); |
| 104 | + state::Model { |
| 105 | + digits, |
| 106 | + boxes: state::Digits(boxes.clone()), |
| 107 | + i, |
| 108 | + nines: nines, |
| 109 | + predigit: predigit, |
| 110 | + have_predigit: have_predigit, |
| 111 | + count: count, |
| 112 | + } |
| 113 | + .into_active_model() |
| 114 | + .reset_all() // we want to update all fields |
| 115 | + .update(&txn) |
| 116 | + .unwrap(); |
| 117 | + |
| 118 | + run_log.pi_digits = Set(s.to_owned()); |
| 119 | + run_log = run_log.save(&txn).unwrap(); |
| 120 | + |
| 121 | + txn.commit().unwrap(); |
| 122 | + } |
| 123 | + |
| 124 | + let mut carry: u32 = 0; |
| 125 | + // work backwards over boxes 1..len-1 |
| 126 | + for j in (1..len).rev() { |
| 127 | + let j_u = j as u32; |
| 128 | + let x = boxes[j] * 10 + carry; |
| 129 | + let q = x / (2 * j_u + 1); |
| 130 | + boxes[j] = x % (2 * j_u + 1); |
| 131 | + carry = q * j_u; |
| 132 | + } |
| 133 | + let x = boxes[0] * 10 + carry; |
| 134 | + let q = (x / 10) as u8; // 0..10 |
| 135 | + boxes[0] = x % 10; |
| 136 | + |
| 137 | + if q == 9 { |
| 138 | + nines += 1; |
| 139 | + } else if q == 10 { |
| 140 | + // increment previous printed digit |
| 141 | + if have_predigit { |
| 142 | + new_digit(&mut s, predigit + 1); |
| 143 | + count += 1; |
| 144 | + } else { |
| 145 | + // first digit becomes 1 |
| 146 | + new_digit(&mut s, 1); |
| 147 | + count += 1; |
| 148 | + have_predigit = true; |
| 149 | + } |
| 150 | + for _ in 0..nines { |
| 151 | + new_digit(&mut s, 0); |
| 152 | + count += 1; |
| 153 | + } |
| 154 | + predigit = 0; |
| 155 | + nines = 0; |
| 156 | + } else { |
| 157 | + if have_predigit { |
| 158 | + new_digit(&mut s, predigit); |
| 159 | + count += 1; |
| 160 | + if count == 1 { |
| 161 | + println!("."); |
| 162 | + } |
| 163 | + } else { |
| 164 | + have_predigit = true; |
| 165 | + } |
| 166 | + predigit = q; |
| 167 | + for _ in 0..nines { |
| 168 | + new_digit(&mut s, 9); |
| 169 | + count += 1; |
| 170 | + } |
| 171 | + nines = 0; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // append last predigit |
| 176 | + new_digit(&mut s, predigit); |
| 177 | + |
| 178 | + println!(); |
| 179 | + run_log.pi_digits = Set(s.to_owned()); |
| 180 | + run_log.save(db).unwrap(); |
| 181 | +} |
| 182 | + |
| 183 | +fn new_digit(s: &mut String, digit: u8) { |
| 184 | + let c = (b'0' + digit) as char; |
| 185 | + s.push(c); |
| 186 | + print!("{c}"); |
| 187 | +} |
| 188 | + |
| 189 | +fn main() { |
| 190 | + pi_spigot(1_000_000); |
| 191 | +} |
0 commit comments