Functions and Classes

Primes!

## Question One:

Write a function to test: How many non-primes untill we get a new largest common factor?

Hint - funtions work like:

def remainder_two(i):
    remainder = i%2
    return(remainder)
j = 4
if remainder_two(j)==0:
    print("divisible by two")

Hint - functions look like this

def Get_Primes(some_range):
    print("I don't know how!")
    results = {"largest prime":2, "Commonest Factor":"eggs","largest factor":2}
    return results
n_to_beat = Get_Primes(100)["largest factor"]
for i in range(100,150):
    new_i = Get_Primes(i)["largest factor"]
    if(new_i > n_to_beat):
        n_to_beat = new_i
        print("new champ",i)

Question Two:

Can you put your function in a .py file and call it from this notebook!

from function import Get_Primes as Extern_Primes
n_to_beat = Extern_Primes(100)["largest factor"]
for i in range(100,150):
    new_i = Extern_Primes(i)["largest factor"]
    if(new_i > n_to_beat):
        n_to_beat = new_i
        print("new champ",i)

Question Three

Finish this class

class Primes_Summary:
    def __init__(self,n):
        self.n = n
        self.primes = self.Get_Primes(n)
        self.largest_factor = self.primes["largest factor"]
        self.n_primes = 
        self.largest_prime = 
    def Print(self):
        print("for the range 1-{}".format(self.n))
        print("the largest prime is {} and the largest factor {}".format(
            self.largest_prime,
            self.largest_factor))
        print("with {} primes in all".format(self.n_primes))
p = Primes_Summary(100)
p.largest_prime
p.Print()

back to edition