 QUESTION

# i still got the last result wrong. How to get it right?

i still got the last result wrong. How to get it right?

if i input 1 5 1000

it supposed to be

These prime factors of highest multiplicity are, from smallest to largest:

[2, 7, 11]

import sys

from random import seed, randint

from math import gcd

from fractions import Fraction

def prime_factors(num):

prime_facts = []

factors = []

i = 2

while i <= num:

if num % i == 0:

num = int(num / i)

factors.append(i)

i = 1

i += 1

for i in factors:

j = 2

prime_flag = True

while j <= i - 1:

if i % j == 0:

prime_flag = False

break

j += 1

if prime_flag:

prime_facts.append(i)

return prime_facts

def main():

try:

arg_for_seed, length, max_value = input('Enter three strictly positive integers: ').split()

except ValueError:

print('Incorrect input, giving up.')

sys.exit()

try:

arg_for_seed, length, max_value = int(arg_for_seed), int(length), int(max_value)

if arg_for_seed < 1 or length < 1 or max_value < 1:

raise ValueError

except ValueError:

print('Incorrect input, giving up.')

sys.exit()

seed(arg_for_seed)

L = [randint(1, max_value) for _ in range(length)]

print('Here is L:')

print(L)

print()

all_possible_fractions = []

for numerator in L:

for denominator in L:

if numerator <= denominator:

all_possible_fractions.append(Fraction(numerator, denominator))

simplest_fraction_size = len(str(max_value)) * 2

complex_fraction_size = 0

for fraction in all_possible_fractions:

if (len(str(fraction.numerator)) + len(str(fraction.denominator))) < simplest_fraction_size:

simplest_fraction_size = len(str(fraction.numerator)) + len(str(fraction.denominator))

if (len(str(fraction.numerator)) + len(str(fraction.denominator))) > complex_fraction_size:

complex_fraction_size = len(str(fraction.numerator)) + len(str(fraction.denominator))

simplest_fractions = []

complex_fractions = []

for fraction in all_possible_fractions:

if (len(str(fraction.numerator)) + len(str(fraction.denominator))) == simplest_fraction_size:

if fraction not in simplest_fractions:

simplest_fractions.append(fraction)

if (len(str(fraction.numerator)) + len(str(fraction.denominator))) == complex_fraction_size:

if fraction not in complex_fractions:

complex_fractions.append(fraction)

print('The size of the simplest fraction <= 1 built from members of L is:',simplest_fraction_size)

print('From smallest to largest, those simplest fractions are:')

for fraction in sorted(simplest_fractions):

print("   %d/%d" % (fraction.numerator, fraction.denominator))

print('The size of the most complex fraction <= 1 built from members of L is:',complex_fraction_size)

print('From largest to smallest, those most complex fractions are:')

for fraction in sorted(complex_fractions, reverse=True):

print("   %d/%d" % (fraction.numerator, fraction.denominator))

complex_denominators = {}

for fraction in complex_fractions:

complex_denominators[fraction.denominator] = prime_factors(fraction.denominator)

primes = complex_denominators[fraction.denominator]

max_count = 0

for prime in primes:

if max_count < primes.count(prime):

max_count = primes.count(prime)

highest_multiplicity = []

for denominator in complex_denominators:

primes = complex_denominators[denominator]

for prime in primes:

if primes.count(prime) <= max_count:

highest_multiplicity.append(prime)

print("The highest multiplicity of prime factors of the latter's denominators is:",max_count)

print('These prime factors of highest multiplicity are, from smallest to largest:')

print('  ', sorted(highest_multiplicity))

if __name__ == '__main__':

main()