import numpy as np import time import os def clear_terminal(): """Clear the terminal screen.""" os.system('cls' if os.name == 'nt' else 'clear') def sieve_of_eratosthenes(limit): """Sieve of Eratosthenes to find all primes up to 'limit'.""" sieve = np.ones(limit // 3 + (limit % 6 == 2), dtype=bool) sieve[0] = sieve[1] = False # 0 and 1 are not prime # Iterate only for numbers that are prime for i in range(1, int(limit**0.5) // 3 + 1): if sieve[i]: prime = 3 * i + 1 sieve[3 * i + prime::prime * 3] = False primes = [2, 3] for i in range(1, len(sieve)): if sieve[i]: primes.append(3 * i + 1) return primes def write_prime_to_file(prime, filename): """Write the hexadecimal and decimal representation of a prime number to the file.""" hex_prime = f"{prime:08X}" # Convert to hex with leading zeros (8 digits) with open(filename, 'a') as f: # Open in append mode f.write(f"{hex_prime} {prime}\n") def main(): limit = 4294967295 filename = 'primes.txt' # Start the sieve calculation and time it start_time = time.time() total_primes = 0 for prime in sieve_of_eratosthenes(limit): total_primes += 1 write_prime_to_file(prime, filename) # Clear the terminal and update information clear_terminal() elapsed_time = time.time() - start_time estimated_time_left = (elapsed_time / total_primes) * (len(primes) - total_primes) if total_primes > 0 else 0 # Print the last prime, elapsed time, estimated time left, and total primes found print(f"Last Prime Found: {prime}") print(f"Total Primes Found: {total_primes}") print(f"Elapsed Time: {elapsed_time:.2f} seconds") print(f"Estimated Time Left: {estimated_time_left:.2f} seconds") # Print final completion message print(f"Finished calculating primes up to {limit}.") print(f"Total Primes Found: {total_primes}") print(f"Total Elapsed Time: {elapsed_time:.2f} seconds") if __name__ == "__main__": main()