Is Infinite Process?
Given integers a
and b
, determine whether the following pseudocode results in an infinite loop
while a is not equal to b do
increase a by 1
decrease b by 1
Assume that the program is executed on a virtual machine which can store arbitrary long numbers and execute forever.
Example
For
a = 2
andb = 6
, the output should beis_infinite_process(a, b) = false
For
a = 2
andb = 3
, the output should beis_infinite_process(a, b) = true
Solution
py
def is_infinite_process(a, b):
return (a > b or a % 2 != b % 2)
print(is_infinite_process(10, 10))
print(is_infinite_process(2, 3)) # TRUE
print(is_infinite_process(2, 6))