PYTHON Write a program in Python with the following: a) Function isPrime : - des
ID: 3810859 • Letter: P
Question
PYTHON
Write a program in Python with the following:
a) Function isPrime:
- description: verifies if a whole number is prime.
- input parameter: whole number.
- return: True or False.
Remark: a prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
b) Function isOdd:
- description: verifies if a whole number is odd.
- input parameter: whole number.
- return: True of False
c) Function power:
- description: calculates the exponentiation of a whole number.
- input parameters: whole number, exponent
- return: number to the power of exponent.
c) Function main:
- description: using functions isPrime, isOdd and power, show the odd prime numbers between the numbers from 1 to 100 (inclusive), and the corresponding exponentiation values: n0, n1, n2 and n3, in a table like the following:
------------------------------------------------------------------
Odd Prime Power of Power of Power of Power of
Number 0 1 2 3
------------------------------------------------------------------
3 1 3 9 27
5 1 5 25 125
11 1 11 121 1331
…
79 1 79 6241 493039
83 1 83 6889 571787
89 1 89 7921 704969
97 1 97 9409 912673
------------------------------------------------------------------
Show the results on the screen.
For the coding, use:
- camel or Hungarian notation
- in-program comments
- logic
- accuracy
- efficiency
Remark: you can't use any Math built-in function.
Explanation / Answer
import pandas as pd
def isPrime(number):
if number>1: #Number is greater than 1.
#Checking for factors
for index in range(2,number):
if((number%index) == 0):
return False
else:
return True
else: #Number is equal to 1.
return False
def isOdd(number):
if number%2 == 0: # Even number
return False
else: #Odd number
return True
def power(number,exponent):
#Using exponentiation to get the answer.
answer = number ** exponent
return answer
def main():
odd_prime = [] # Get the list of all odd prime numbers.
dictionary = {}
for i in range(1,100):
if(isPrime(i) and isOdd(i)):
odd_prime.append(i)
for value in odd_prime:
dictionary[value] = [power(value,0),power(value,1),power(value,2),power(value,3)]
return dictionary
mydict=main()
df=pd.DataFrame.from_dict(mydict) #Putting dictionary value to dataframe.
df=df.T
df.columns=[ 'Power of 0', 'Power of 1', 'Power of 2', 'Power of 3']
df.index.names = ['Odd Prime Number']
print(df)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.