1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
/// This is a method that allows calculate a prime number using a hash. /// The idea is really simple: we generate a number n passing the hash from 16-basis to a special basis, /// taken the value of tha char in 10-basis and multiply by 2^i, where i is the position of the char in the string. /// This number n, we calculate the rest module 31, obtaining an other number d. We find the numbers x=2^d-1 and y=2^(d+1)-1. /// We call a function that returns the first prime nuber in [x,y]. This will be our choice. /// /// We apply module 31 for 2 reasons: /// 1. Every prime generate will be lower than 2^32. /// 2. We can work with up 2^512. If every prime is lower than 2^32, we can use 16 or more hashes. /// /// Exemples of output /// --------------------------------------------------------------------------------------------------------------------------- /// ba0eb08fb6e22be90efdddbcc4e01e6b /// /// 14 /// /// PRIME:16411 /// ----------------------------------------------------------------------------------------------------------------------------- /// 59d979a35b796a359ca93b5551543989 /// /// 10 /// /// PRIME:1031 /// --------------------------------------------------------------------------------------------------------------------------- /// c1db9ac31e24338f84d6478cd240c469 /// /// 10 /// /// PRIME:1031 /// --------------------------------------------------------------------------------------------------------------------------- /// 8cc7f325e1f85c4495ed1d5dd9241322 /// /// 20 /// /// PRIME:1048583 /// --------------------------------------------------------------------------------------------------------------------------- /// ccf5c961e56ea0f5ed9fd2ad708bb222 /// /// 12 /// /// PRIME:4099 /// --------------------------------------------------------------------------------------------------------------------------- /// e321b05a921a43e28a047c8d3f3badfe /// /// 0 /// /// PRIME:2147483647 /// --------------------------------------------------------------------------------------------------------------------------- /// f64db73f55c13d867ff371bdff1cd535 /// /// 14 /// /// PRIME:16411 /// --------------------------------------------------------------------------------------------------------------------------- /// 41f0d73e282a545a986a32c435307141 /// /// 10 /// /// PRIME:1031 /// --------------------------------------------------------------------------------------------------------------------------- /// 61aead180d629200cc69792ca8152415 /// /// 14 /// /// PRIME:16411 /// --------------------------------------------------------------------------------------------------------------------------- /// e93f049d72d96884c3d35dda9b9c5f3f /// /// 18 /// /// PRIME:262147 /// --------------------------------------------------------------------------------------------------------------------------- /// c44f76fcb1a959281f4abe02cb1b1352 /// /// 0 /// /// PRIME:2147483647 /// --------------------------------------------------------------------------------------------------------------------------- /// c44f76fcb1a959281f4abe02cb1b1352 /// /// 0 /// /// PRIME:2147483647 /// --------------------------------------------------------------------------------------------------------------------------- /// 907faf1c31f729fd2fdb6e66d2b300f2 /// /// 0 /// /// PRIME:2147483647 /// --------------------------------------------------------------------------------------------------------------------------- /// 116071665641408661462957134870906380377375371356177661370878538945945071899 /// /// This is the product of all primes /// use std::u128; extern crate rand; use rand::Rng; pub fn hash_to_prime(v2: Vec<&str>) -> u128{ let mut count:u128; let mut seed:u128; let mut res:u128; let mut n:u32; n=0; res=0; let n3=v2.len() as u32; for element in v2.clone(){ count=hex(element); seed=count*u128::pow(2,(n3-1)-n); res=(res+seed)%31; n=n+1; } println!("{}", res); if res==0{ res=31; } let r = rand::thread_rng().gen_range(1, 101); let mut x=u128::pow(2,res as u32)-1; if x==0{ x=1; } let y=u128::pow(2, (res+1) as u32)-1; let z:u128; z=private_find_prime(x, y, r); return z //println!("PRIME:{}", z); } fn hex (a: &str) -> u128 { let mut count:u128; count = 0; if a == "0"{ count = 0; } if a == "1"{ count = 1; } if a == "2"{ count = 2; } if a == "3"{ count = 3; } if a == "4"{ count = 4; } if a == "5"{ count = 5; } if a == "6"{ count = 6; } if a == "7"{ count = 7; } if a == "8"{ count = 8; } if a == "9"{ count = 9; } if a == "a" || a=="A"{ count = 10; } if a == "b" || a=="B"{ count = 11; } if a == "c" || a=="C"{ count = 12; } if a == "d" || a=="D"{ count = 13; } if a == "e" || a=="E"{ count = 14; } if a == "f" || a=="F"{ count = 15; } if a == "\0"{ count=0; } count } fn private_find_prime (x: u128, y: u128, r: u32) -> u128{ let mut n:u128; let mut i=0; let mut c; let mut d:u32; d=0; println!("r:{}", r); loop{ if x>=8388607{ n=x+i; c=its_prime(n); if c==1{ break; }else{ i=i+1; } }else{ n=x+i; c=its_prime(n); if c==1{ d=d+1; i=i+1; if d==r{ break; } }else{ i=i+1; } } } if n==y{ n=0; } if x==1{ n=2; } n as u128 } fn its_prime (x : u128) -> i32{ let mut coun=0; let n; for i in 2 .. x-1{ if x%i == 0{ coun=coun+1; } } if coun == 0{ n=1; }else{ n=0; } n }