Advertisement
Guest User

Untitled

a guest
Dec 6th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.53 KB | None | 0 0
  1. extern crate primal;
  2. extern crate bmp;
  3. extern crate num;
  4. extern crate num_bigint;
  5.  
  6. use std::io;
  7. use std::io::Write;
  8. use std::fs::File;
  9. use std::ops::Index;
  10. use std::fmt::Error;
  11. use std::num::ParseIntError;
  12. use std::collections::{HashMap, LinkedList};
  13.  
  14. use primal::*;
  15. use num::{Integer, BigInt};
  16. use num_bigint::Sign;
  17. use bmp::Pixel;
  18.  
  19. static RSA100C: &'static str = "1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139";
  20. static RSA100C_N: &'static str = "14387588531011964456730684619177102985211280936";
  21. static RSA100A: &'static str = "37975227936943673922808872755445627854565536638199";
  22. static RSA100B: &'static str = "40094690950920881030683735292761468389214899724061";
  23. static RSA100D: &'static str = "39020571855401265512289573339484371018905006900194";
  24. static RSA100E: &'static str = "61218444075812733697456051513875809617598014768503";
  25. static RSA100F: &'static str = "16822699634989797327123095165092932420211999031886";//2d+1-e
  26. static RSA100N: &'static str = "14387588531011964456730684619177102985211280936";
  27. static RSA100X: &'static str = "1045343918457591589480700584038743164339470261995";
  28. static RSA100X_PLUS_N: &'static str = "1059731506988603553937431268657920267324681542931";
  29.  
  30. pub trait BmpColor {
  31. fn color(e: i32, n: i32, f: i32, img: &mut bmp::Image, pixel: Pixel);
  32. }
  33.  
  34. impl BmpColor for TheEnd {
  35. fn color(e: i32, n: i32, f: i32, img: &mut bmp::Image, pixel: Pixel) {
  36. img.set_pixel((e + 1024) as u32, n as u32, pixel);
  37. img.set_pixel((f + 1024) as u32, (n - 1) as u32, pixel);
  38. }
  39. }
  40. pub trait Hamilton {
  41. fn calculate_z(h: Hamiltonian) -> i32;
  42. fn hamiltonian_product(first: Hamiltonian, second: Hamiltonian) -> i32;
  43. }
  44. pub struct Hamiltonian{ a: i32, b: i32, c: i32, d: i32, i: i32, j: i32, k: i32 }
  45. pub struct CoordHamiltonian { e: i32, n: i32, hamiltonian: Hamiltonian }
  46.  
  47. impl Hamilton for TheEnd {
  48. fn calculate_z(h: Hamiltonian) -> i32 {
  49. // calcualate Z
  50. (h.b * h.i) + (h.c * h.j) + (h.d * h.k)
  51. }
  52. fn hamiltonian_product(first: Hamiltonian, second: Hamiltonian) -> i32 {
  53. (first.a * second.a) + ((first.b * second.b) * first.i) + ((first.c * second.c) * first.j) + ((first.d * second.d) * first.k)
  54. + ((first.b * second.a) * first.i) + ((first.b * second.b) * (first.i * first.i)) + ((first.b * second.c) * (first.i * first.j)) + ((first.b * second.d) * (first.i * first.k))
  55. + ((first.c * second.a) * first.j) + ((first.c * second.b) * (first.j * first.i)) + ((first.c * second.c) * (first.j * first.j)) + ((first.c * second.d) * (first.j * first.k))
  56. + ((first.d * second.a) * first.k) + ((first.d * second.b) * (first.k * first.i)) + ((first.d * second.c) * (first.k * first.j)) + ((first.d * second.d) * (first.k * first.k))
  57. }
  58. }
  59. #[derive(Debug)]
  60. pub struct TheEnd(pub HashMap<i32, HashMap<i32, LinkedList<String>>>);
  61.  
  62. impl TheEnd {
  63. pub fn new() -> TheEnd {
  64. TheEnd(HashMap::new())
  65. }
  66. pub fn create_the_end(&mut self) {
  67. let i_max: i32 = 512;
  68. let x_min: i32 = 0;
  69. let y_min: i32 = 0;
  70. let x_max: i32 = 64;
  71. let y_max: i32 = 64;
  72. let mut img = bmp::Image::new(2048, 512);
  73. let mut color = Pixel { r:255, g:000, b:000 };
  74. let mut red = Pixel { r:255, g:000, b:000 };
  75. let mut blue = Pixel { r:000, g:000, b:255 };
  76. let mut green = Pixel { r:000, g:255, b:000 };
  77. let mut white = Pixel { r:255, g:255, b: 255 };
  78. let mut black = Pixel { r: 000, g: 000, b: 000 };
  79. let mut orange = Pixel { r:255, g: 165, b: 000 };
  80. let mut purple = Pixel { r:255, g: 000, b: 255 };
  81. let mut yellow = Pixel { r:255, g: 255, b: 000 };
  82. let mut colors: Vec<Pixel> = Vec::new();
  83. colors.push(red);
  84. colors.push(blue);
  85. colors.push(green);
  86. colors.push(orange);
  87. colors.push(yellow);
  88. colors.push(purple);
  89. for i in 1 .. i_max {
  90. for j in 1 .. i {
  91. let a: i32 = i - j;
  92. let b: i32 = i + j;
  93. let c: i32 = a * b;
  94. let odd = c % 2 == 1;
  95. let d = (c as f64).sqrt() as i32;
  96. let e = c - (d * d) + 1;
  97. let f = e - ((2 * d) + 1);
  98. let n = i - d;
  99. let x = d - a;
  100. let k = i * j;
  101. let hamiltonian = Hamiltonian { a, b, c, d, i, j, k };
  102. let z = TheEnd::calculate_z(hamiltonian);
  103. let primes: Vec<u32> = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149].to_vec();
  104. colors.push(red);
  105. colors.push(blue);
  106. colors.push(green);
  107. //,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149].to_vec();
  108. let mut count = 1;
  109. let base = a + b/ a;
  110. for (color, prime) in colors.iter().cycle().zip(primes) {
  111. if a % base == prime as i32 {
  112. TheEnd::color(e,n,f,&mut img, *color);
  113. }
  114. }
  115. if e == 1 {
  116. TheEnd::color(e,n,f,&mut img, white);
  117. }
  118. if a % base == 1 {
  119. TheEnd::color(e,n,f,&mut img, blue);
  120. }
  121. if a % base == 3 {
  122. TheEnd::color(e,n,f,&mut img, green);
  123. }
  124. if a % base == 5 {
  125. TheEnd::color(e,n,f,&mut img, red);
  126. }
  127. /*
  128. if a % base == 7 {
  129. TheEnd::color(e,n,f,&mut img, purple);
  130. }
  131. if a % base == 11 {
  132. TheEnd::color(e,n,f,&mut img, yellow);
  133. }
  134. */
  135. if primal::is_prime(c as u64) {
  136. TheEnd::color(e,n,f,&mut img, white);
  137. }
  138.  
  139. let text = format!("{{{}:{}:{}:{}:{}:{}}}", e, n, d, x, a, b);
  140. self.0
  141. .entry(e)
  142. .or_insert(HashMap::new())
  143. .entry(n)
  144. .or_insert(LinkedList::new())
  145. .push_back(text);
  146. let text = format!("{{{}:{}:{}:{}:{}:{}}}", f, n - 1, d + 1, x + 1, a, b);
  147. self.0
  148. .entry(f)
  149. .or_insert(HashMap::new())
  150. .entry(n - 1)
  151. .or_insert(LinkedList::new())
  152. .push_back(text);
  153. }
  154. }
  155. let _ = img.save("img.bmp");
  156. }
  157. pub fn output<W: Write>(&self, target: &mut W) -> io::Result<()> {
  158. let i_max = 256;
  159. let x_min = -64;
  160. let y_min = 0;
  161. let x_max = 64;
  162. let y_max = 64;
  163. let set_s = 12;
  164.  
  165. for y in 0 .. y_max {
  166. for z in 0 .. set_s {
  167. for x in x_min .. x_max {
  168. match self.0.get(&x).and_then(|e| e.get(&y)).and_then(|e| e.iter().nth(z as usize)) {
  169. Some(x) => write!(target, "{},", x)?,
  170. None => write!(target, ",")?,
  171. }
  172. }
  173. write!(target, "\n")?
  174. }
  175. }
  176. Ok(())
  177. }
  178. }
  179.  
  180. #[derive(Debug)]
  181. pub struct TheEndRecord {
  182. // column and remainder
  183. e: i32,
  184. // row and midpoint
  185. n: i32,
  186. d: i32,
  187. x: i32,
  188. a: i32,
  189. b: i32,
  190. c: Option<i32>,
  191. }
  192.  
  193. impl TheEndRecord {
  194. fn new() -> TheEndRecord {
  195. TheEndRecord { e: 0, n: 0, d: 0, x: 0, a: 0, b: 0, c: None }
  196. }
  197. }
  198. pub trait Solver {
  199. fn generate_ter(c: i32, n: i32) -> Self;
  200. fn solve_na(x: i32, e: i32) -> i32;
  201. fn solve_n(x: i32, e: i32, a: i32) -> i32;
  202. fn solve_ed(c: i32) -> (i32, i32);
  203. fn solve_f(e: i32, d: i32) -> i32;
  204. fn solve_x(d: i32, n: i32, c: i32) -> i32;
  205. fn solve_a(d: i32, x: i32) -> i32;
  206. fn solve_b(c: i32, a: i32) -> i32;
  207. fn solve_c(&mut self);
  208. }
  209.  
  210. impl Solver for TheEndRecord {
  211. fn generate_ter(c: i32, n: i32) -> Self {
  212. let mut ter = TheEndRecord::new();
  213. let ed = TheEndRecord::solve_ed(c);
  214. let f = TheEndRecord::solve_f(ed.0, ed.1);
  215. let x = TheEndRecord::solve_x(ed.1, n, c);
  216. let a = TheEndRecord::solve_a(ed.1, x);
  217. let b = TheEndRecord::solve_b(c, a);
  218. ter.e = ed.0;
  219. ter.n = n;
  220. ter.d = ed.1;
  221. ter.x = x;
  222. ter.a = a;
  223. ter.b = b;
  224. ter.c = Some(c);
  225. ter
  226. }
  227. fn solve_na(x: i32, e: i32) -> i32 {
  228. (x^2 + e)/2
  229. }
  230. fn solve_n(x: i32, e: i32, a: i32) -> i32 {
  231. ((x * x) + e )/ (2 * a)
  232. }
  233. // returns (e,d)
  234. fn solve_ed(c: i32) -> (i32, i32) {
  235. let c = (c as f64).sqrt();
  236. let rem = (c as f64).sqrt() % 1.0;
  237. (c as i32, rem as i32)
  238. }
  239. fn solve_f(e: i32, d: i32) -> i32 {
  240. e - ((2 * d) + 1).abs()
  241. }
  242. fn solve_x(d: i32, n: i32, c: i32) -> i32 {
  243. ((((d + n) * (d + n) - c) - n) as f64).sqrt() as i32
  244. }
  245. fn solve_a(d: i32, x: i32) -> i32 {
  246. d - x
  247. }
  248. fn solve_b(c: i32, a: i32) -> i32 {
  249. c / a
  250. }
  251. fn solve_c(&mut self) {
  252. let c = self.a * self.b;
  253. self.c = Some(c);
  254. }
  255. }
  256. pub trait Maps {
  257. fn prime_map(vec: Vec<TheEndRecord>) -> HashMap<i32, HashMap<i32, LinkedList<TheEndRecord>>>;
  258. }
  259. impl Maps for TheEndRecord {
  260. fn prime_map(vec: Vec<TheEndRecord>) -> HashMap<i32, HashMap<i32, LinkedList<TheEndRecord>>> {
  261. let mut hashmap: HashMap<i32, HashMap<i32, LinkedList<TheEndRecord>>> = HashMap::new();
  262. for ter in vec {
  263. let c = ter.c.unwrap();
  264. if primal::is_prime(c as u64) {
  265. hashmap.entry(c as i32)
  266. .or_insert(HashMap::new())
  267. .entry(c)
  268. .or_insert(LinkedList::new())
  269. .push_back(ter);
  270. }
  271. }
  272. hashmap
  273. }
  274. }
  275. fn main() {
  276. let mut the_end = TheEnd::new();
  277. the_end.create_the_end();
  278.  
  279. // let mut buffer = File::create("./output.csv").unwrap();
  280. // the_end.output(&mut buffer).unwrap();
  281. let five: f64 = 5.0;
  282. let sqrt = five.sqrt();
  283. let gr = (1.0 + sqrt) / 2.0;
  284. let recip = 1.0 / gr;
  285. println!("{:?}", gr);
  286. println!("{:?}", recip);
  287. let mut ter_vec: Vec<TheEndRecord> = Vec::<TheEndRecord>::new();
  288. for x in -64 .. 64 {
  289. for y in -64 .. 64 {
  290. the_end.0.get(&x).unwrap().get(&y).map(|e| {
  291. for entry in e {
  292. let vec: Vec<&str> = entry.split(|c| c == '{' || c == ':' || c == '}').collect();
  293. let mut ter = TheEndRecord {
  294. e: vec.index(1).parse().unwrap(),
  295. n: vec.index(2).parse().unwrap(),
  296. d: vec.index(3).parse().unwrap(),
  297. x: vec.index(4).parse().unwrap(),
  298. a: vec.index(5).parse().unwrap(),
  299. b: vec.index(6).parse().unwrap(),
  300. c: None,
  301. };
  302.  
  303. ter.solve_c();
  304. ter_vec.push(ter);
  305. }
  306. });
  307. }
  308. }
  309.  
  310.  
  311. let prime_map = TheEndRecord::prime_map(ter_vec);
  312. for entries in prime_map {
  313. println!("{:?}", entries.0);
  314. }
  315. }
  316.  
  317. // 2an = xx + e
  318. // n = xx + e / 2a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement