Difference between revisions of "Prime numbers assignment"
From WLCS
(5 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
'''Resources:''' | '''Resources:''' | ||
+ | * [[Media:PythonWhileLoops.pptx]] | ||
* [http://openbookproject.net/thinkcs/python/english2e/ch06.html HTTLACS: Ch 6 - Iteration] | * [http://openbookproject.net/thinkcs/python/english2e/ch06.html HTTLACS: Ch 6 - Iteration] | ||
* [http://www.wikihow.com/Check-if-a-Number-Is-Prime How to check if a number is prime] | * [http://www.wikihow.com/Check-if-a-Number-Is-Prime How to check if a number is prime] | ||
Line 9: | Line 10: | ||
# Prompt the user to enter a number, '''N''' | # Prompt the user to enter a number, '''N''' | ||
# Create a variable named '''isPrime''' and set it to '''True''' | # Create a variable named '''isPrime''' and set it to '''True''' | ||
− | # Use a loop that counts from 2 up | + | # Use a loop that counts from 2 up through '''N/2''' |
− | # | + | #* If N is divisible by the loop counter, then set '''isPrime''' to '''False''' and '''break''' out of the loop |
− | + | #* In order to check for divisibility, you can get the remainder by using the modulus (%) operator. Example: 13 % 5 yields 3 because the remainder of 13 / 5 is 3 | |
+ | #* Compare the remainder to 0 to see if it is evenly divided | ||
# After the loop, check if '''isPrime''' is '''True''', | # After the loop, check if '''isPrime''' is '''True''', | ||
## print out that it is a prime number, | ## print out that it is a prime number, |
Latest revision as of 13:55, 28 November 2017
Objective:
- To become well-learned in the way of the while loop
Resources:
Directions:
- Prompt the user to enter a number, N
- Create a variable named isPrime and set it to True
- Use a loop that counts from 2 up through N/2
- If N is divisible by the loop counter, then set isPrime to False and break out of the loop
- In order to check for divisibility, you can get the remainder by using the modulus (%) operator. Example: 13 % 5 yields 3 because the remainder of 13 / 5 is 3
- Compare the remainder to 0 to see if it is evenly divided
- After the loop, check if isPrime is True,
- print out that it is a prime number,
- else, print out that it is not a prime number
Additional Challenges:
- Prompt the user for A
- Print out the first A prime numbers