Python Program
Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate the diameter of the earth. For several decades he served as the director and chief librarian of the famous library in Alexandria. He was highly regarded in the ancient world, but unfortunately only fragments of his writing have survived.
The algorithm described for this assignment is known as the Sieve of Eratosthenes. The algorithm is designed to find all prime numbers within a given range in an interesting way - instead of testing each number to see if it is prime, it assumes that all numbers are prime. It then finds all "non-prime" numbers and marks them as such. When the algorithm is finished you are left with a list of numbers along with their designation (Prime or Not Prime). Here's a detailed outline of how the algorithm works:
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
P |
P |
P |
P |
P |
P |
P |
P |
P |
P |
P |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
N |
N |
P |
P |
P |
P |
P |
P |
P |
P |
P |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
N |
N |
P |
P |
N |
P |
N |
P |
N |
P |
N |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
N |
N |
P |
P |
N |
P |
N |
P |
N |
N |
N |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
N |
N |
P |
P |
N |
P |
N |
P |
N |
N |
N |
Here is your task:
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 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 Found 168 prime numbers!
Notes on Efficiency:
Efficiency is important here - here are some hints:
For more information about this algorithm (and an animated representation of it running) check out the wikipedia article: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Explanation::
Code in PYTHON::
import math
'''
Declaring the variable named n to store the value entered by user
'''
n=0
'''
Following while loop ask user to enter value until
valid input is not entered
'''
while True:
n=int(input("Enter the value of n : "))
if(n>=10):
break
print("Invalid number! At least 10.")
'''
Now we create a list named number[]
and initialize it to hold 'P' for n+1 elements
'''
number = []
for i in range(n+1):
number.insert(i,'P')
'''
Initialize first two number elements as 'N'
'''
number[0]='N'
number[1]='N'
'''
Now we mark all the even numbers are 'N' except 2
'''
for i in range(4,n+1,2):
number[i]='N'
'''
An crosslimit is declared below which will be used to
stop processing ahead numbers like if n=1000 then no need
to check multiple of 800
'''
crosslimit=int(math.sqrt(n))
for i in range(3,crosslimit+1,2):
if number[i]=='P':
for j in range(i*i,n+1,2*i):
number[j]='N'
'''
Now we print the numbers that are prime
'''
j=int(1)
for i in range(len(number)):
if number[i]=='P':
if j%10==0:
j=1
print('{:<5}'.format(i))
else:
j=j+1
print('{:<5}'.format(i),end="")
print()
OUTPUT::
Enter the value of n : 9
Invalid number! At least 10.
Enter the value of n : 1000
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 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229
233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349
353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463
467 479 487 491 499 503 509 521 523 541
547 557 563 569 571 577 587 593 599 601
607 613 617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719 727 733
739 743 751 757 761 769 773 787 797 809
811 821 823 827 829 839 853 857 859 863
877 881 883 887 907 911 919 929 937 941
947 953 967 971 977 983 991 997
Please provide the feedback!!
Thank You!!
Python Program Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate...
The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite lie., not prime) the multiples of each prime, starting with the multiples of 2 The sieve of Eratosthenes can be expressed in pseudocode, as follows: Input: an integer n Let A be an array of 8oo1ean values, indexed by integers 2 to n, initially all set to true. for t - 2, 3,...
PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than 1 that is evenly divisible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: Create a list with all elements initialized to 1 (true). List elements with prime indexes will remain 1. All other elements will eventually be set to zero. Starting with list element 2, every time a list element is found...
One way to find prime numbers less than n is to treat them as array indices. Mark each position as True initially, assuming that all numbers are prime. Then, starting with index 2, mark all multiples of 2 (greater than 2) as not prime (False). Repeat this for multiples of 3, then multples of 4, etc. When the algorithm terminates, only prime indices will still be True; everything else will be False. The following function takes an integer n as...
Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...
in visual studio build a masm program that prints out the prime numbers in a array L1001-Sieve of Eratosthenes Please use your textbook as a reference. Goal: Use what we have learned to generate prime numbers. Prime numbers have many applications in computer science and as such, efficient ways to discover prime numbers can be very useful. Mathematicians have been intrigued by the concept for ages including the Greek mathematician, Eratosthenes of Cyrene (famous for calculating the circumference o the...
Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the console (inclusive). The identified prime numbers must be stored in an array . The inputted number should be between 0 and 1000 (inclusive). array is large enough to o Make sure your hold all the prim e numbers. Dynamic memory allocation is not required for this assignment, so you can have unused space in your array Make sure you can handle the cases of...
Write and test a MIPS assembly language program to compute and display the first prime numbers up to n where n is given. Set n to be 19 but the program should work of any value of n. For the program to identify primes, the easiest way is to use the algorithm: for (i = 2; i < x; i++) if ((x % i) == 0) break; //break out, not prime where x is the number you are checking...
Problem 6: Take an infinite set A of naturals, i.e. A CN. Create the choice function : by setting o n) = n-th integer in A. For example, if A is the set of prime numbers, then 0 CA(1) = 3, A(2) = 5, OA(3) = 7, 7A(4) = 11, ect. We also define the position function A:A N as follows. Given an integer ne A, we denote by An) = position of nin A. Again, if A is the...
Problem 6: Take an infinite set A of naturals, i.e. AC N. Create the choice function aA : NN by setting oA(n) = n-th integer in A. For example, if A is the set of prime numbers, then oA(0) 2, OA(1) 3, aA(2) = 5, oA(3) = 7, OA(4) = 11, ect. We also define the position function TA : AN as follows. Given an prime numbers, then TA(11) = 4 because 11 is the 4th prime number when we...
Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following ingredients (some of them you developed for earlier assignments): 1. A method to generate random binary numbers with n-digits (hint: for the most significant digit, you have no choice, it will be 1; similarly, for the least significant digit there is no choice, it will have to be 1; for all other position, generate 0 or 1 at random) 2. A method to compute...