Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import math
- print("""Select one option:
- 1. LCM of integers from 1 to user-defined N.
- 2. LCM of integers in a user-defined range.
- 3. LCM of a set of user-defined integers.
- """)
- choice = str(input('Input: '))
- def calc_lcm(arr):
- lcm = arr[0]
- for i in arr[1:]:
- lcm = int(lcm * i / math.gcd(lcm, i))
- return lcm
- if choice == '1':
- in_num = int(input('Upper bound: '))
- arr = []
- for i in range(in_num):
- arr.append(i+1)
- lcm = calc_lcm(arr)
- print(f'LCM integers from 1 to {in_num} = {lcm}')
- elif choice == '2':
- lower_bound = int(input('Lower bound: '))
- upper_bound = int(input('Upper bound: '))
- arr = list(range(lower_bound, (upper_bound + 1)))
- lcm = calc_lcm(arr)
- print(f'LCM of integers from {lower_bound} to {upper_bound} = {lcm}')
- elif choice == '3':
- arr = []
- while True:
- try:
- arr.append(int(input('Enter an integer or type "c" to quit and calculate LCM: ')))
- except ValueError:
- break
- lcm = calc_lcm(arr)
- out_num = ','.join(str(x) for x in arr)
- print(f'LCM of ({out_num}) = {lcm}')
- else:
- print('Select between options 1, 2 and 3.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement