Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- A friend of mine takes a sequence of numbers from 1 to n (where n > 0).
- Within that sequence, he chooses two numbers, a and b.
- He says that the product of a and b should be equal to the sum of all numbers in the sequence, excluding a and b.
- Given a number n, could you tell me the numbers he excluded from the sequence?
- '''
- # Function 1
- def removeABfromSum(n):
- if n <= 1 or n != int(n):
- print("Error while using the function '" + removeABfromSum.__name__ + "'")
- return -1000000
- SUM = n * (n + 1) / 2
- solA = -1000
- solB = -1000
- for a in range(1, n+1):
- b = (SUM - a) / (a + 1)
- if b == int(b) and b > a and b <= n:
- # That means that the pair (a,b) is a solution of this problem
- # The value of a and b remain! So, I will break the loop in order to gain time
- solA = int(a)
- solB = int(b)
- break
- return int(SUM), solA, solB
- # Function 2
- def prettyPrint(n):
- SUM, solA, solB = removeABfromSum(n)
- if solA == -1000 and solB == -1000:
- print("n = " + str(n) + " ----> sum = " + str(SUM) + " ----> No solutions a,b")
- else:
- print("n = " + str(n) + " ----> sum = " + str(SUM) + " ----> a = " + str(solA) + ", b = " + str(solB))
- # MAIN FUNCTION
- nList = list(range(2, 101))
- for n in nList:
- prettyPrint(n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement