User:RaiseAfloppaFan3925/Program forms
I hope this ends up in the correct namespace...
User-friendly iterative Fibonacci program
This program calculates the given Fibonacci number using the iterative method instead of the classic recursive method. However, it must have a user-friendly interface that loops asking the user forever until an exit is requested. Additionally, if the language's numeric type of choice won't be able to EXACTLY represent the Fibonacci value (for example, with floating-point precision errors in JavaScript and its languages TypeScript, Bussin/Bussin X, Yappacino, and Nonstraightforward, or with integer size limits), it must print Resulting value would be too large. Try again. for any input that would produce an out-of-range Fibonacci number.
Outcomes
Here are the outcomes for all possible user inputs.
Any non-negative number
Example with the 40th Fibonacci number
Input a number: 40 F(40) = 102234155
Any negative number
Example with -39
Input a number: -39 Cannot get a negative Fibonacci number. Try again.
help
Input a number: help 'help' to see this 'exit' to exit
exit
Exits the program.
any non-command, non-numeric inputs
Input a number: 重音テト Invalid number. Try again. Input a number: NaN Invalid number. Try again. Input a number: --help Invalid number. Try again.
Examples
🍡Dango
Golfed, works with 0.11.0 interpreter and up (previous 0.10.0 interpreter has an issue in :io-input where the newline (carriage return and newline on Windows) is not removed)
(j)(11)(while)(=)(')(exit)---- fetch 0 fetch 0 (:io-input)(0)---- eat (')(Input a number: )----
(j)(1)---- eat eat eat eat (')('exit' to exit)('c)(10)(')('help' to see this)('c)(10)---- (while)(=)(')(help)----
(j)(1)---- eat eat (')(Invalid number. Try again.)('c)(10)---- (while)(=)()---- fetch 0 (`)
(j)(1)---- eat eat (')(Cannot get a negative Fibonacci number. Try again.)('c)(10)(while)(<)(0)---- fetch 0
(j)(1)---- eat eat (')(Resulting value would be too large. Try again.)('c)(10)(while)(>)(92)---- fetch 0
(j)(10)(0)(while)(=)(0)---- fetch 0
(j)(10)(1)(while)(=)(1)---- fetch 0
(1)(1)(0)---- fetch 0
(j)(9)(while)(<)---- fetch 4 fetch 0 (+)(1)---- fetch 4 fetch 2 fetch 3 fetch 4 (+)---- fetch 2 fetch 2
(j)(1)---- eat ('c)(10)---- eat eat eat ('c)(41)(')( = )---- eat eat (')(F()----
Python
def fib(n):
if n == 0: return 0
previous = 0
current = 1
for i in range(1, n):
next = previous + current
previous = current
current = next
return current
while True:
string = input("Input a number: ")
if string == 'exit': break
elif string == 'help':
print("'help' to see this")
print("'exit' to exit")
else:
try:
value = int(string)
if value < 0:
print('Cannot get a negative Fibonacci number. Try again.')
else:
print(f'F({value}) = {fib(value)}')
except:
print("Invalid number. Try again.")
Golfed
def f(n):
a=0;b=1;i=1
while i<n:c=a+b;a=b;b=c;i+=1
return b if n>0 else 0
while 1:
a=input("Input a number: ")
match a:
case'exit':break
case'help':print("'help' to see this\n'exit' to exit")
case _:
try:b=int(a);print('Cannot get a negative Fibonacci number. Try again.'if b<0 else f'F({b}) = {f(b)}')
except:print("Invalid number. Try again.")