Question

In number theory, he introduced a scheme whose pseudocode is as below. Convert this to Python and explain (line by line) what

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

What the code does:
Finds the prime numbers below the given number n.

Python Code:

import math

def scheme(n):

A = [] #empty array

#add n elements, initially all are True

for i in range(n+1):

A.append(True)

#Now iterate from 2 to sqrt(n)

for i in range(2, int(math.sqrt(n))+1):

#if A[i] is True

if A[i] is True:

#change the values to False at positions from i*i to n, incrementing by i

#so all factors of i from i*i are set to False

for j in range(i**2, n+1, i):

A[j] = False

#Now list contains the True values only for prime numbers which don't have factors

#A[2:] is the list from index 2 to end

return A[2:]

#Test Case 1

A = scheme(100) #call function with 100

print("Primes from 2-100")

#print indices from 2 which are True

for i in range(len(A)):

if A[i] == True:

print(i+2, end=' ') #indexing from 2

print()

#Test Case 2

A = scheme(1000) #call function with 100

print("Primes from 2-1000")

#print indices from 2 which are True

for i in range(len(A)):

if A[i] == True:

print(i+2, end=' ') #indexing from 2

print()

OUTPUT:

Primes from 2-100 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 Primes from 2-1000 2 3 5 7 11 13 17

Add a comment
Know the answer?
Add Answer to:
In number theory, he introduced a scheme whose pseudocode is as below. Convert this to Python and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to...

    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,...

  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    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,...

  • **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the...

    **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the creatures from the classic science-fiction film "The Day of the Triffids") is an algorithm that enciphers a plaintext message by encoding each letter as a three-digit number and then breaking up and rearranging the digits from each letter's encoded form. For this assignment, you will create a set of Python functions that can encode messages using this cipher (these functions...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT