Post Reply 
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): 
    if(
b==0): 
        return 

    
else: 
        return 
gcd(b,a%b

54 
24

print(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!
Find all posts by this user
Quote this message in a reply
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:

#cas
gcd2(tempa,tempb):=
BEGIN
LOCAL a,b,T;
a:=tempa;
b:=tempb;
T:="";
WHILE (a<>0 AND b<>0) DO
   T:=T+" "+"("+ a + "," + b + ")"; 
      IF(a<b) THEN
      b := b MOD a; 
      ELSE
      a := a MOD b;
      END;
   END;
   RETURN T+"
   "+gcd(tempa,tempb);
END;
#end
For other modular arithmetic for hp prime you can find on my github: https://github.com/fmorea/aritmetica-modulare
Find all posts by this user
Quote this message in a reply
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
Find all posts by this user
Quote this message in a reply
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)
Find all posts by this user
Quote this message in a reply
04-01-2023, 06:14 PM
Post: #5
RE: Problem with Code for GCD of Two Numbers
(04-01-2023 07:54 AM)FernandoMorea Wrote:  No need to use python in your hp prime:

I think Rimi is simply playing around with Python and is now stuck with an error for which there is no obvious reason. Python itself of course has the gcd() function in the respective module("arit").

Günter
Find all posts by this user
Quote this message in a reply
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:

>>> gcd((a,b))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: gcd() takes exactly 2 arguments (1 given)

As stated above, the code runs fine. Where do you see the double parentheses?

Günter
Find all posts by this user
Quote this message in a reply
04-01-2023, 06:51 PM
Post: #7
RE: Problem with Code for GCD of Two Numbers
(04-01-2023 06:23 PM)Guenter Schink Wrote:  As stated above, the code runs fine. Where do you see the double parentheses?

It was just a guess, that may cause Python to generate missing argument error.

(03-31-2023 12:36 PM)Rimi Wrote:  TypeError: gcd() missing 1 required positional argument: 'b'
Find all posts by this user
Quote this message in a reply
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?

It was just a guess, that may cause Python to generate missing argument error.

(03-31-2023 12:36 PM)Rimi Wrote:  TypeError: gcd() missing 1 required positional argument: 'b'

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 Smile

Günter

BTW for Python a typical 1-liner:
PHP Code:
def gcd(a,b): return if a==else gcd(b%aa
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)