1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
coldgirl [10]
2 years ago
7

The Sieve of Eratosthenes is an elegant algorithm for finding all the prime numbers up to some limit n. The basic idea is to fir

st create a list of numbers from 2 to n. The first number is removed from the list, and announced as prime, and all multiples of this number is removed up to n are removed from the list. This process continues until the list is empty.
For example, if you wanted to find all the prime numbers up to 10, the list would contain 2, 3, 4, 5, 6, 7, 8, 9, 10. The 2 is removed and announced to be prime and 4, 6, 8, and 10 are removed since they are multiples of 2. That leaves 3, 5, 7, 9., repeat the process until the list is empty.
Write a program that prompts a user for n and then uses the sieve algorithm to find all the prime numbers less than or equal to n.
Computers and Technology
1 answer:
IRISSAK [1]2 years ago
4 0

Answer:

Check the explanation

Explanation:

#!usr/bin/python

#FileName: sieve_once_again.py

#Python Version: 2.6.2

#Author: Rahul Raj

#Sat May 15 11:41:21 2010 IST

 

fi=0 #flag index for scaling with big numbers..

n=input('Prime Number(>2) Upto:')

s=range(3,n,2)

def next_non_zero():

"To find the first non zero element of the list s"

global fi,s

while True:

if s[fi]:return s[fi]

fi+=1

def sieve():

primelist=[2]

limit=(s[-1]-3)/2

largest=s[-1]

while True:

m=next_non_zero()

fi=s.index(m)

if m**2>largest:

primelist+=[prime for prime in s if prime] #appending rest of the non zero numbers

break

ind=(m*(m-1)/2)+s.index(m)

primelist.append(m)

while ind<=limit:

s[ind]=0

ind+=m

s[s.index(m)]=0

#print primelist

print 'Number of Primes upto %d: %d'%(n,len(primelist))

if __name__=='__main__':

sieve()

You might be interested in
Consider the following statements regarding computer programs A - Variables can contain different values at different times.B -
jeka94

Answer:

The answer is: Only A is correct.

Explanation:

Variables in a program can assume different values at different times, and the program can then produce different results, depending on circumstances, so A is correct.

In a computer language, a reserved word (also known as a reserved identifier) is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use". This is a syntactic definition, and a reserved word may have no meaning. So, B is incorrect.

Hence, the answer is: Only A is correct.

6 0
2 years ago
cellPhoneBill(m,tx)Write the function cellPhoneBill()that takes two int m and txas input and returns a float.The function takes
lidiya [134]

Answer:

The solution code is written in C++

  1. float cellPhone(int m, int tx){
  2.    float COST_PER_MIN = 0.1;
  3.    float COST_PER_MESSAGE = 0.2;
  4.    
  5.    float bill_amount = m * COST_PER_MIN + tx * COST_PER_MESSAGE;
  6.    
  7.    return bill_amount;
  8. }

Explanation:

Firstly, declare a function named cellPhone() that takes two input parameters, m and tx (Line 1).

Since the policy of the carrier company is not given in the question, I make a presumption that the cost per minutes is $0.10 and the cost per message is $0.20 (Line 2- 3).

Next, apply the formula m * COST_PER_MIN + tx * COST_PER_MESSAGE to calculate the total bill (Line 5) and return the bill_amount as function output (Line 7).

4 0
3 years ago
Different between anlog and digital computer?​
sp2606 [1]

Explanation:

Analog computer:

It works with naturaland physical values

It works upon continuous data.

Its accuracy is low.

Digital computer:

It works with digits.

It works upon discrete data.

Its accuracy is high

3 0
3 years ago
In order to customize the status bar, you must
Marianna [84]

The answer is:

D) Click on the icon with the horizontal bars.

8 0
3 years ago
Read 2 more answers
Can some one answer this
andreev551 [17]
SUP THANKS FOR THE FREE POINTS
7 0
2 years ago
Other questions:
  • Your game design company has recently asked all employees to use a specific personal information management application (PIM) to
    6·1 answer
  • What human activity would cause a decrease in the population of another species? (The action of humans would impact one species,
    9·1 answer
  • An array subscript can be an expression, but only as long as the expression evaluates to what type?
    7·1 answer
  • Insecurely attached infants who are left my their mothers in an unfamiliar setting often will
    14·1 answer
  • Only class b and class c networks can be subnetted true or false
    6·1 answer
  • Bobbi owns a protein shake company, which has been trading for 3 years. Her products are stocked in a number of gyms, but the co
    11·1 answer
  • Search engines enable you to
    12·1 answer
  • In order to restrict editing to a document, a user will go to Review, , Restrict Editing, and will then select what kinds of edi
    12·1 answer
  • Business use a fax cover sheet is to
    12·1 answer
  • Two types of storage memory​
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!