Problem with Code for GCD of Two Numbers
|
03-31-2023, 12:36 PM
Post: #1
|
|||
|
|||
Problem with Code for GCD of Two Numbers
Hi all,
I'm having an issue with a code I'm trying to write for the GCD of two numbers. I'm following this example from this problem post but I'm getting an error that I can't seem to figure out. Here is the code I'm using: PHP Code: def gcd(a,b): When I run the code, I get the following error: TypeError: gcd() missing 1 required positional argument: 'b' Any help would be greatly appreciated. Thanks! |
|||
04-01-2023, 07:54 AM
(This post was last modified: 04-01-2023 07:54 AM by FernandoMorea.)
Post: #2
|
|||
|
|||
RE: Problem with Code for GCD of Two Numbers
No need to use python in your hp prime:
(note, I'm defining gcd2 because the function gcd is already existent) Code:
|
|||
04-01-2023, 05:48 PM
Post: #3
|
|||
|
|||
RE: Problem with Code for GCD of Two Numbers
I typed in your little snipped. It answers 6 correctly, no error.
Is this the only program currently in the python app? Günter |
|||
04-01-2023, 05:57 PM
Post: #4
|
|||
|
|||
RE: Problem with Code for GCD of Two Numbers
The bug is likely a typo ... like this:
>>> gcd((a,b)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: gcd() takes exactly 2 arguments (1 given) |
|||
04-01-2023, 06:14 PM
Post: #5
|
|||
|
|||
RE: Problem with Code for GCD of Two Numbers | |||
04-01-2023, 06:23 PM
Post: #6
|
|||
|
|||
RE: Problem with Code for GCD of Two Numbers
(04-01-2023 05:57 PM)Albert Chan Wrote: The bug is likely a typo ... like this: As stated above, the code runs fine. Where do you see the double parentheses? Günter |
|||
04-01-2023, 06:51 PM
Post: #7
|
|||
|
|||
RE: Problem with Code for GCD of Two Numbers | |||
04-01-2023, 07:12 PM
Post: #8
|
|||
|
|||
RE: Problem with Code for GCD of Two Numbers
(04-01-2023 06:51 PM)Albert Chan Wrote:(04-01-2023 06:23 PM)Guenter Schink Wrote: As stated above, the code runs fine. Where do you see the double parentheses? Sorry no offense intended. Yes of course, there may be a typo somewhere, but the code he published doesn't have one. I simply copied it to the virtual Prime and it executed without any error. The error is hidden somewhere else. We'll work on it Günter BTW for Python a typical 1-liner: PHP Code: def gcd(a,b): return b if a==0 else gcd(b%a, a) |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)