Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 5 - Loops - Programming Exercises - Page 161: 5.22

Answer

code

Work Step by Step

# 5.22 (Display prime numbers between 2 and 1,000) Modify Listing 5.13 to display all # the prime numbers between 2 and 1,000, inclusive. Display eight prime numbers # per line. NUMBER_OF_PRIMES = 1000 # Number of primes to display NUMBER_OF_PRIMES_PER_LINE = 8 # Display 10 per line count = 0 # Count the number of prime numbers number = 2 # A number to be tested for primeness print("The first 1000 prime numbers are") # Repeatedly find prime numbers while number < NUMBER_OF_PRIMES: # Assume the number is prime isPrime = True # Is the current number prime? # Test if number is prime divisor = 2 while divisor <= number / 2: if number % divisor == 0: # If true, the number is not prime isPrime = False # Set isPrime to false break # Exit the for loop divisor += 1 # Display the prime number and increase the count if isPrime: count += 1 # Increase the count print(format(number, '5d'), end='') if count % NUMBER_OF_PRIMES_PER_LINE == 0: # Display the number and advance to the new line print() # Jump to the new line # Check if the next number is prime number += 1
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.