Python Notes
- Simple input command
- Input Command and, Integer and Float functions
- Loops in Python
- Basics of IF...THEN commands
Copy the following code and paste it into Python (optional - but useful)
# removing the extra spaces inside the quotes
print ("I will add two numbers.")
print ()
first=45
second=90
total=first+second
print ("The first number =",first)
print ('The second number =',second)
print ()
print ("Their total is",total,'.')
print ()
print ('Oops - Hannah does not like that extra space!')
print ('I hope this is better.')
print ()
print ('Their total is ',total,'.',sep='') # the sep='' or sep=blank space removes the spaces inside the quotes.
# However, the programmer must now enter the extra spaces where needed when using the sep='' command.
Remember to login to your Repl.it account for complete assignment instructions.
Prime Number Coding Assignment Help
Variable |
Definition |
a |
the number entered by the user, i.e. the one we will determine if it is prime or not |
GF |
Greatest Factor other than the number itself which turns out to be half of the a |
factors |
The number of factors for a |
divisor |
An increasing number that will divide a |
quotient |
The quotient of a divided by the divisor |
test |
The integer of quotient – used to determine if a/divisor is a whole number |
We need to remember; a prime number is divisible ONLY by 1 and itself. The largest factor of any number other than the number itself is half of the original number. Therefore, we need to create a loop structure that will divide the given number until we reach the halfway point. This can be done using a structure as shown in the pseudo-code below:
- # start of program
- input the user number with the variable a
- determine the greatest factor: GF=a/2
- we set the first factor and the initial divisor, i.e., factors=1 and divisor=1
- the loop to search for factors begin – in this case, I will use a while loop structure
- loop while divisor is less than or equal to GF
- determine the quotient: quotient=a/divisor
- test the quotient to see if it is a whole number: test=integer of quotient
- if the quotient is an integer, i.e., quotient=test
- then we increase the count of factors: factors=factors+1
- we then go to our next divisor: divisor=divisor+1
- after the loop is finished, we can determine if the user number is prime by the number of factors
- if the number of factors is greater than 2: factors>2
- then the user number is not a prime
- else (meaning if the factors less than or equal to 2)
- the number is prime
- # end of program
Within the loop structure, you can add print statement to display each factor found. Some of the large numbers might take a while so you can place within the loop an “escape” after ten factors are found and tell the user that you will only display the first ten by using a module like below:
- if the number of factors is greater than 10
- set the divisor higher than the greatest factor: divisor=GF+1 (nullifies the while condition)