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]
3 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]3 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
To move from layout view to form view, tap or click the _____ button on the access status bar.
bija089 [108]
To move from layout view to form view, tap or click the _____ button on the access status bar.
View button
8 0
3 years ago
Why is color theory important
pychu [463]
The Importance of Color Theory in Painting. Color Theory was a required foundations course in the Visual Communication Design program at BYU, so all the design students took it–the interior designers, graphic designers, industrial designers, and the illustrators.
3 0
3 years ago
Read 2 more answers
The gene form of a trait is called a(n) ​
Rom4ik [11]

Answer:

alleles

Explanation:

7 0
3 years ago
15 points. Please give an actual answer and not some random thing. this is not just free points. Correct answer will receive bra
trapecia [35]

Answer:

i think its b

Explanation:

i did the test and got it right

7 0
3 years ago
Read 2 more answers
for java ?(Business: check ISBN-13)ISBN-13 is a new standard for identifying books. It uses 13 digits d1d2d3d4d5d6d7d8d9d10d11d1
ludmilkaskok [199]

Answer:

In Java:

import java.util.*;

public class Main{

public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 String isbn;

 System.out.print("First 1:2 digits: ");

 isbn = input.nextLine();

 if(isbn.length()==12){

 int chksum = 0;

 for(int i = 0; i<12;i++){

     if((i+1)%2==0){      chksum+= 3 * Character.getNumericValue(isbn.charAt(i));          }

     else{          chksum+=Character.getNumericValue(isbn.charAt(i));          }  }

 chksum%=10;

 chksum=10-chksum;

 if(chksum==10){

 System.out.print("The ISBN-13 number is "+isbn+"0");}

 else{

 System.out.print("The ISBN-13 number is "+isbn+""+chksum);          }   }

 else{

     System.out.print("Invalid Input");

 }     }}

Explanation:

See attachment for explanation where comments are used to explain each line

Download txt
6 0
3 years ago
Other questions:
  • Switches operate on what layer of the OSI Model
    11·1 answer
  • A troubleshooter's ability to design and test hypotheses in order to solve a technology problem is based on ____.
    14·1 answer
  • Who has access to the source code of proprietary software
    6·1 answer
  • The four key stages with regards to data visualization workflow. Select one key stage and explain it briefly about that stage.St
    10·1 answer
  • The CUSTOMERS and SALES tables contain these columns:
    6·1 answer
  • Are the buying and selling of stocks centralized activities? Why or why not?
    10·2 answers
  • ________ allows the computer to get its configuration information from the network instead of the network administrator providin
    5·1 answer
  • Suppose you are developing a data visualization application DataViz which calls some functions from a statistics library class S
    5·1 answer
  • Viruses which activate themselves after a specific time is called
    10·2 answers
  • Como se llaman las herramientas y maquinas pque utilizan para hacer cosas artesanales
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!