"""Takes an integer as input, k, as input and finds the sum of all primes below k."""

def is_prime(number):
    #if no number smaller than or equal to the squareroot divides a number it is prime
    upper_bound = math.floor(math.sqrt(number))
    for i in range(2, upper_bound+1):
        if number % i == 0:
            return False
    #if we get here, the number was prime
    return True

print("""I shall sum primes below what number?""")
number = input("> ")

k = check_input_integer(number)

#we wish to only check odd numbers for primes so we do 2 separately
if k == 1 or k==2:
    print(0)
    exit(0)
elif k == 3:
    print(2)
    exit(0)

potential_prime = 1
sum = 2

while potential_prime < k-2:
    potential_prime += 2
    if is_prime(potential_prime):
Example #2
0
#if so, then we have found composite's smallest prime factor.  We may divide
#composite by this factor and run through the program again.  We do this
#until composite is prime (and thus we have our final answer)
def largest_prime_factor(composite, starter):
    #eliminates an abusrd case
    if composite == 1:
        print("This is not divisible by any prime.")
        exit(0)

#once we have divided by i, we know that no smaller primes divide composite
#by construction of the algorithm.  Thus we may tell the future iteration
#of the recurrsion to ignore these numbers.
    i = starter
    upper_bound = math.floor(math.sqrt(composite))
    while i <= upper_bound:
        if composite % i == 0:
            new_composite = composite // i
            return largest_prime_factor(new_composite, i)
        else:
            i += 1
    #if we get here, the number was prime
    return composite

print(
    "Please give me a positive integer.  I will find its largest prime factor."
)
number = input("> ")

composite = check_input_integer(number)
print(largest_prime_factor(composite, 2))
Example #3
0
from check_integer import check_input_integer
"""
Find the sum of all the multiples of 3 or 5 below 1000.
We shall generalize to give the answer for a user input.
"""

print(
    "I shall find the sum of all multiples of 3 or 5 below what positive integer?"
)
number = input("> ")

upper_bound = check_input_integer(number)

#finds how many items we need to sum
upper_bound_five = (
    upper_bound - 1) // 5  #if the input is 1000, we want to avoid adding 1000
upper_bound_three = (upper_bound - 1) // 3
upper_bound_fifteen = (upper_bound - 1) // 15

sum = 0
for i in range(upper_bound_five):
    sum += (i + 1) * 5  #+1 makes it goe from 1 to upper_bound_five
for j in range(upper_bound_three):
    sum += (j + 1) * 3
#this gets rid of what we have double counted
for k in range(upper_bound_fifteen):
    sum -= (k + 1) * 15
print(sum)
Example #4
0
13 adjancent numbers.  The program needs the final line in the text to be blank.
"""

script, filename = argv

txt = open(filename)

reading = 1 #keeps track of if the file has concluded
array_of_digits = []
#converts our file to an array of digits
while(reading == 1):
    sequence = txt.readline()
    if sequence == '':
        reading = 0
    else:
        line = check_input_integer(sequence)
        for i in range(sequence.__len__()-1):
            array_of_digits.append(line % (10**(sequence.__len__()-1-i)) // 10**(sequence.__len__()-2-i))

biggest = 0
biggest_array = [0,0,0,0,0,0,0,0,0,0,0,0,0]
for i in range(array_of_digits.__len__()-12):
    product = 1
    for j in range(13):
        product = product * array_of_digits[i+j]
    if biggest < product:
        biggest = product
        for k in range(13):
            biggest_array[k] = array_of_digits[i+k]

print(f"{biggest} is the product of {biggest_array}")