primes/primes.rs
1//! LAST ACTUALITZATION: INCREASE THE SPEED
2//!
3//! # Explaining
4//!
5//! This is a method that allows calculate a prime number using a hash.
6//! The idea is really simple: we generate a number n passing the hash from 16-basis to a special basis,
7//! 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.
8//! 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.
9//! We call a function that returns the first prime nuber in [x,y]. This will be our choice.
10//!
11//! We apply module 31 for 2 reasons:
12//! 1. Every prime generate will be lower than 2^{32}.
13//! 2. We can work with up 2^{512}. If every prime is lower than 2^{32}, we can use 16 or more hashes.
14//!
15//! ```toml
16//!
17//! [dependencies]
18//! Gen_Prime = "1.1.5"
19//!
20//! ```
21//!
22//! # Examples
23//!
24//! -------------------------------------------------------------------------------------------------------
25//! EXAMPLE 1
26//!
27//! ```rust
28//! extern crate primes;
29//!
30//!
31//! # fn main() {
32//! let z:u128;
33//! println!("Introduce a hash");
34//! let mut a = String::new();
35//! std::io::stdin().read_line(&mut a).expect("Fail");
36//! let v : Vec<&str>=a.split("").collect();
37//! z=primes::hash_to_prime(v);
38//! println!("{}", z);
39//!
40//! # }
41//!
42//! ```
43//! Outputs:
44//! -------------------------------------------------------------------------------------------------------
45//!
46//! Introduce a hash
47//!
48//! a123dfe4758bc27237a
49//!
50//! 2147483647
51//!
52//! -------------------------------------------------------------------------------------------------------
53//!
54//! Introduce a hash
55//!
56//! 074a4a47c445cf604b2ca687fed09fd8f3a78a16d20786b1a97aa6642fe0f87a8577a52cbace36bab6a6c40c3f1843be
57//!
58//! 1073741827
59//!
60//! -------------------------------------------------------------------------------------------------------
61//!
62//! Introduce a hash
63//!
64//! 00eccf559792ee42e3ea26e394e9ab52ce569e47504d44715102cc0c2ab4f542dc54c383e8e13d360ebc57c5ede5e64b
65//!
66//! 16777259
67//!
68//! -------------------------------------------------------------------------------------------------------
69//!
70//! Introduce a hash
71//!
72//! daa41c63b728ba70e6c377d7d17d9e53185fc720e7e6326626608eb1edc3735f67d963c303d92c9196583f3cd8739943
73//!
74//! 16553
75//!
76//! -------------------------------------------------------------------------------------------------------
77//!
78//! ## EXAMPLE 2
79//!
80//! ```rust
81//! use std::io;
82//! use std::u128;
83//! extern crate bigint;
84//! extern crate primes;
85//! extern crate rand;
86//! use bigint::U512;
87//!
88//! # fn main() {
89//! let mut mult:bigint::U512;
90//! mult=U512::one();
91//! loop{
92//! println!("Introduce a hash");
93//! let mut a = String::new();
94//! std::io::stdin().read_line(&mut a).expect("Fail");
95//! let v : Vec<&str>=a.split("").collect();
96//! let n2=v.len() as u32;
97//! let res:u128;
98//! let mut v2=Vec::with_capacity((n2-3) as usize);
99//! for i in 0..n2-3 {
100//! i as usize;
101//! v2.push(v[(i+1) as usize]);
102//! } //this lines are to safe each char in one component of a vector, and to delete white spaces
103//! res=primes::hash_to_prime(v2); //Generating a prime
104//! mult=mult*(bigint::U512::from(res as usize)); //Product of the primes
105//! println!("Do you want to enter another hash?[Y/N]");
106//! let mut b = String::new();
107//! io::stdin().read_line(&mut b).expect("Fail");
108//! let b : char = b.trim().parse().expect("Please, input one choice");
109//! if b=='N' || b=='n'{
110//! break;
111//! }
112//! }
113//! println!("{}", mult);
114//! # }
115//!
116//! ```
117//!
118//! ## Output
119//!
120//! Introduce a hash
121//!
122//! daa41c63b728ba70e6c377d7d17d9e53185fc720e7e6326626608eb1edc3735f67d963c303d92c9196583f3cd8739943
123//!
124//! 19
125//!
126//! r:100
127//!
128//! Do you want to enter another hash?[Y/N]
129//!
130//! Y
131//!
132//! Introduce a hash
133//!
134//! 0907b5ff1b7d6f7b2639ca00565c8f42cbf7529b992f825b2676ec30c294b477b913476d6a508da9d195f1dacc893173
135//!
136//! 23
137//!
138//! r:39
139//!
140//! Do you want to enter another hash?[Y/N]
141//!
142//! Y
143//!
144//! Introduce a hash
145//!
146//! 4cb34fd780fbac405a433f428bd5672c0f7003cf1ae9d0675225c8d83783c0fc57e8b1f64a9bad799084f71381d1115e
147//!
148//! 27
149//!
150//! r:55
151//!
152//! Do you want to enter another hash?[Y/N]
153//!
154//! Y
155//!
156//! Introduce a hash
157//!
158//! fae89ae85b629f1d5263c7b5ad5035cc72c3cb02f95a9a242f5065f235611863f28fd4798818054e4cf69fd464e7f3b0
159//!
160//! 8
161//!
162//! r:49
163//!
164//! Do you want to enter another hash?[Y/N]
165//!
166//! Y
167//!
168//! Introduce a hash
169//!
170//! d0054fddab35b4aa7ab1356811f5c5ef7a1536ccca1ac76549b36030c875c4329d12f86b67591e3d9b3ca7f178861c94
171//!
172//! 5
173//!
174//! r:57
175//!
176//! Do you want to enter another hash?[Y/N]
177//!
178//! N
179//!
180//! 110277345636720260972118701
181//!
182//! where the number after each hash is the value mod 31 (d) and r is a rand number that calculates the rth prime in [x,y].
183//! The last number is the product of all primes. Can you find one of the primes? ;)
184//!
185 use std::u128;
186 extern crate rand;
187 use rand::Rng;
188
189 pub fn hash_to_prime(v2: Vec<&str>) -> u128{
190 let mut count:u128;
191 let mut seed:u128;
192 let mut res:u128;
193 let mut n:u32;
194 n=0;
195 res=0;
196 let n3=v2.len() as u32;
197 for element in v2.clone(){
198
199 count=hex(element);
200 seed=count*u128::pow(2,(n3-1)-n);
201 res=(res+seed)%31;
202 n=n+1;
203 }
204 println!("{}", res);
205 if res==0{
206 res=31;
207 }
208 let r = rand::thread_rng().gen_range(1, 101);
209 let mut x=u128::pow(2,res as u32)-1;
210 if x==0{
211 x=1;
212
213 }
214 let y=u128::pow(2, (res+1) as u32)-1;
215 let z:u128;
216 z=private_find_prime(x, y, r);
217 return z
218 //println!("PRIME:{}", z);
219
220 }
221 fn hex (a: &str) -> u128 {
222 let mut count:u128;
223 count = 0;
224 if a == "0"{
225 count = 0;
226 }
227 if a == "1"{
228 count = 1;
229 }
230 if a == "2"{
231 count = 2;
232 }
233
234 if a == "3"{
235 count = 3;
236 }
237 if a == "4"{
238 count = 4;
239 }
240 if a == "5"{
241 count = 5;
242 }
243 if a == "6"{
244 count = 6;
245 }
246 if a == "7"{
247 count = 7;
248 }
249 if a == "8"{
250 count = 8;
251 }
252 if a == "9"{
253 count = 9;
254 }
255 if a == "a" || a=="A"{
256 count = 10;
257 }
258 if a == "b" || a=="B"{
259 count = 11;
260 }
261 if a == "c" || a=="C"{
262 count = 12;
263 }
264 if a == "d" || a=="D"{
265 count = 13;
266 }
267 if a == "e" || a=="E"{
268 count = 14;
269 }
270 if a == "f" || a=="F"{
271 count = 15;
272 }
273 if a == "\0"{
274 count=0;
275 }
276 count
277 }
278 fn private_find_prime (x: u128, y: u128, r: u32) -> u128{
279 let mut n:u128;
280 let mut i=0;
281 let mut c;
282 let mut d:u32;
283 d=0;
284 println!("r:{}", r);
285 loop{
286 if x>=8388607{
287 n=x+i;
288 c=its_prime(n);
289 if c==1{
290
291 break;
292 }else{
293 i=i+1;
294 }
295 }else{
296 n=x+i;
297 c=its_prime(n);
298 if c==1{
299 d=d+1;
300 i=i+1;
301 if d==r{
302 break;
303 }
304 }else{
305 i=i+1;
306 }
307 }
308 }
309 if n==y{
310 n=0;
311 }
312 if x==1{
313 n=2;
314 }
315 n as u128
316 }
317 fn its_prime (x : u128) -> i32{
318 let mut coun=0;
319 let n;
320 let mut i= 3;
321 if x%2==0{
322 n=0;
323 return n;
324 }
325 while i < x{
326
327 if x%i == 0{
328 coun=coun+1;
329 }
330 i=i+2;
331 }
332 if coun == 0{
333 n=1;
334 }else{
335 n=0;
336 }
337 n
338 }