Fix prime calculator; more threads by default
This commit is contained in:
		
							parent
							
								
									71138258e6
								
							
						
					
					
						commit
						099012a60b
					
				
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -1,3 +1,6 @@ | ||||
| /target | ||||
| report.json | ||||
| primes.json | ||||
| primes1.json | ||||
| primes1.txt | ||||
| primes1.zip | ||||
|  | ||||
							
								
								
									
										11
									
								
								Cargo.toml
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								Cargo.toml
									
									
									
									
									
								
							| @ -9,14 +9,15 @@ edition = "2021" | ||||
| num = "0.4" | ||||
| thread-priority = "1.1" | ||||
| rand = "0.8" | ||||
| num-bigint= { default-features = false, version = "0.4" } | ||||
| num-bigint = { default-features = false, version = "0.4" } | ||||
| once_cell = "1.19" | ||||
| 
 | ||||
| [profile.release] | ||||
| lto = true          # Enable link-time optimization | ||||
| codegen-units = 1   # Reduce number of codegen units to increase optimizations | ||||
| panic = 'abort'     # Abort on panic | ||||
| strip = true        # Strip symbols from binary; remove pdb | ||||
| lto = true              # Enable link-time optimization | ||||
| codegen-units = 1       # Reduce number of codegen units to increase optimizations | ||||
| panic = 'abort'         # Abort on panic | ||||
| strip = true            # Strip symbols from binary; remove pdb | ||||
| overflow-checks = false | ||||
| 
 | ||||
| [profile.dev] | ||||
| opt-level = 3 | ||||
							
								
								
									
										24
									
								
								check_primes.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								check_primes.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,24 @@ | ||||
| const assert = require("assert"); | ||||
| 
 | ||||
| const primes_generated = require("./primes.json"); | ||||
| const correct_primes = require("./primes1.json"); | ||||
| 
 | ||||
| assert( | ||||
|   primes_generated.length == correct_primes.length, | ||||
|   "Number of primes generated is not equal to the number of correct primes", | ||||
| ); | ||||
| 
 | ||||
| for (let i = 0; i < primes_generated.length; i++) { | ||||
|   if (primes_generated[i] !== correct_primes[i]) { | ||||
|     console.error( | ||||
|       `Incorrect prime at index ${i}: ${primes_generated[i]} | ${correct_primes[i]}`, | ||||
|     ); | ||||
|     process.exit(1); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| console.log("All primes are correct"); | ||||
| console.log( | ||||
|   "Last prime in generated primes: ", | ||||
|   primes_generated[primes_generated.length - 1], | ||||
| ); | ||||
							
								
								
									
										28
									
								
								convert_primelist.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								convert_primelist.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,28 @@ | ||||
| import json | ||||
| 
 | ||||
| def convert_primes_to_json(file_path): | ||||
|     # Step 1: Read the file content | ||||
|     with open(file_path, 'r') as file: | ||||
|         lines = file.readlines() | ||||
| 
 | ||||
|     primes = [] | ||||
|     for line in lines: | ||||
|         # Step 2: Extract prime numbers | ||||
|         # Split each line into words and filter out non-numeric entries | ||||
|         numbers = [int(num) for num in line.split() if num.isdigit()] | ||||
|         primes.extend(numbers) | ||||
| 
 | ||||
|     # Step 3: Convert the list of primes to a JSON array | ||||
|     primes_json = json.dumps(primes, indent=4) | ||||
| 
 | ||||
|     return primes_json | ||||
| 
 | ||||
| # Example usage: | ||||
| file_path = 'primes1.txt' | ||||
| primes_json = convert_primes_to_json(file_path) | ||||
| print(primes_json) | ||||
| 
 | ||||
| # If you want to save the JSON array to a file: | ||||
| output_path = 'primes1.json' | ||||
| with open(output_path, 'w') as output_file: | ||||
|     output_file.write(primes_json) | ||||
| @ -17,7 +17,12 @@ pub mod prime_utils { | ||||
|             return true; | ||||
|         } | ||||
| 
 | ||||
|         if number.sqrt().pow(2) == *number { | ||||
|         let sqrtnum = number.sqrt(); | ||||
| 
 | ||||
|         if sqrtnum.pow(2) == *number { | ||||
|             if number == &BigUint::from(23u8) { | ||||
|                 eprintln!("SQRT check failed"); | ||||
|             } | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
| @ -31,21 +36,23 @@ pub mod prime_utils { | ||||
|             let one = BigUint::one(); | ||||
|             let zero = BigUint::zero(); | ||||
| 
 | ||||
|             let sqrtnum = number.sqrt() + &one; //fake ceil function
 | ||||
|             let sqrtnum = sqrtnum + &one; //fake ceil function
 | ||||
| 
 | ||||
|             for prime in g_primes { | ||||
|                 if prime < &sqrtnum && number % prime == zero { | ||||
|                     if number == &BigUint::from(23u8) { | ||||
|                         eprintln!("Prime table check failed"); | ||||
|                     } | ||||
|                     return false; | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             if !is_probably_prime(number, 5) { | ||||
|                 return false; | ||||
|             } | ||||
| 
 | ||||
|             loop { | ||||
|                 i += &one; | ||||
|                 if number % &i == zero { | ||||
|                     if number == &BigUint::from(23u8) { | ||||
|                         eprintln!("manual check failed"); | ||||
|                     } | ||||
|                     return false; | ||||
|                 } | ||||
|                 if i == sqrtnum { | ||||
| @ -61,6 +68,12 @@ pub mod prime_utils { | ||||
|             last = (last.pow(2) - &two) % number; | ||||
|         } | ||||
| 
 | ||||
|         last == BigUint::from(0u8) | ||||
|         let ret = last == BigUint::from(0u8); | ||||
| 
 | ||||
|         if number == &BigUint::from(23u8) && !ret { | ||||
|             eprintln!("RET check failed"); | ||||
|         } | ||||
| 
 | ||||
|         ret | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -65,8 +65,8 @@ fn main() { | ||||
|     const N: i64 = 1_000_000; | ||||
|     const NF: f64 = N as f64; | ||||
|     const NU: usize = N as usize; | ||||
|     const THREADS: usize = 12; | ||||
|     const SPLITTER: u64 = N as u64 / 10; | ||||
|     const THREADS: usize = 30; | ||||
|     const SPLITTER: u64 = N as u64 / THREADS as u64; | ||||
| 
 | ||||
|     try_set_thread_priority(); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user