-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
38 lines (38 loc) · 1.3 KB
/
Copy pathscript.py
File metadata and controls
38 lines (38 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import math
def find_numbers(sum, product):
discrim=sum**2-4*product
if discrim<0:
return None
sqrt_d=math.sqrt(discrim)
x=(sum+sqrt_d)/2
y=(sum-sqrt_d)/2
if x.is_integer() and y.is_integer() and x>0 and y>0:
return int(x),int(y)
else:
return None
print("This program finds two numbers that add to a specified sum and multiply to a specified product.")
print("Let x, and y be natural numbers larger than 0.")
while True:
try:
sumval=int(input("Enter the value they should add up to: "))
if sumval<=0:
print("Invalid value entered. Please enter a natural number.")
continue
break
except ValueError:
print("Invalid value entered. Please enter a natural number.")
while True:
try:
prdval=int(input("Enter the value they should multiply to: "))
if prdval<=0:
print("Invalid value entered. Please enter a natural number.")
continue
break
except ValueError:
print("Invalid value entered. Please enter a natural number.")
result=find_numbers(sumval, prdval)
if result is None:
print("No natural numbers satisfy the conditions.")
else:
x,y = result
print(f"x and y are {x} and {y} respectively.")