Is my code for gcd function valid?

  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    Code Function Gcd
Click For Summary

Discussion Overview

The discussion revolves around the validity of a C program implementing the greatest common divisor (gcd) function, as well as its application in checking for Pythagorean triplets. Participants are sharing code snippets, suggesting improvements, and addressing specific cases in the implementation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant presents an initial implementation of the gcd function using Euclid's algorithm but questions its correctness.
  • Another participant modifies the code, suggesting that it now works but does not address cases where there is no remainder, leading to incorrect gcd results.
  • A third participant provides a standalone program that correctly handles the case of no remainder by utilizing the modulus function, indicating a potential improvement over the previous implementations.
  • Subsequent posts introduce a new program aimed at finding n-th Pythagorean triplets, with a participant expressing difficulty in enforcing the condition a < b < c within nested loops.
  • Another participant shares an abbreviated version of the triplet-checking program, seeking hints on where to correctly implement the condition for ordering the triplets.

Areas of Agreement / Disagreement

Participants express varying opinions on the correctness of the gcd function implementations, with some suggesting improvements while others identify specific cases that remain unaddressed. The discussion on the triplet-checking program also shows uncertainty about the correct placement of conditions, indicating that no consensus has been reached on the best approach.

Contextual Notes

Limitations include unresolved mathematical steps in the gcd function implementations and the need for clearer conditions in the triplet-checking program. The discussions reflect a range of assumptions about the behavior of the code and the mathematical properties being utilized.

MathematicalPhysicist
Science Advisor
Gold Member
Messages
4,662
Reaction score
372
i have to write the function gcd for c programme.
here's my code:
Code:
int gcd(int m, int n)
{
 int s,r,x,y;
 
 if (m>=n)
  {
    r=m-(m/n)*n;
    for(x=m,y=n;r!=0;x=y,y=r)
     r=x-(x/y)*y;
   }
  if(n>m)
  {
    r=n-(n/m)*n;
  for(x=n,y=m;r!=0;x=y,y=r)
      r=x-(x/y)*y;
 }
return(r);
}
so is this correct? I am using the fact that the divison of two integers returns an integer solution, i.e without the remainder, and ofcourse euclid's algorithm.
p.s
this is a part of a programme that i need to do in order to check for pythaogrean triplets, so it's not supposed to run alone.

thanks in advance.
 
Technology news on Phys.org
Code:
int gcd(int m, int n)
{
 int s,r,x,y;
 
 if (m>=n)
  {
    r=m-(m/n)*n;
    for(x=m,y=n;r!=0;x=y,y=r)
     {
      s=r;
     r=x-(x/y)*y;
      }
   }
  if(n>m)
  {
    r=n-(n/m)*n;
  for(x=n,y=m;r!=0;x=y,y=r)
      {
      s=r;
      r=x-(x/y)*y;
      }
 }
return(s);
}
i think that now it works ok, what do you think?
 
Yea, its working. But you need to add a case that covers when there is no remainder. Like when I do 10 and 5, the program returns 0 as the gcd.

Heres a stand alone program that I wrote, utilizing the modulus function.
Code:
/ * Euclid's method of finding the greatest common divisor of two integers*/
#include <stdio.h>

int gcd(int num1, int num2);
int swap(int *pa, int *pb);

int main()
{
	int m,n,gcd_num;
	
	printf("Enter two integers\n");
	scanf("%d %d", &m, &n);
	if(m <= 0 || n <= 0) {
		printf("Zero input detected\n");
		exit(1);
	}
	if(m <= n) swap(&m, &n);
	gcd_num = gcd(m,n);
	printf("\nThe greatest common divisor of %d and %d is %d\n",m,n,gcd_num);
	return 0;
}

int swap(int *pa, int *pb)
{
	int temp = *pa;
	*pa = *pb;
	*pb = temp;
	return;
}

int gcd(int m, int n)
{
	int r;
	
	[b]if(m%n == 0)
	r = n;[/b]
	
	while((m%n!=0))
	{
   	r = m%n;
   	printf("Trying %d\n",r);
	m=n;
	n=r;
	}
	return r;
}
The bold lines takes care of the case when there is no remainder.
 
Last edited:
now i need to write a programme that will get as input, 'n' and 'max' it should look on triples such that 1<=a<b<c<=max and check wether they satisfy a^n+b^n=c^n, if so the triple will be printed, and at the end there will be printed the number of triples.
i need no duplicates of triples such as: 3,4,5 or 4,3,5 which are the same and that gcd(a,b)=1.
now he i wrote the programme, but I am finding difficult where to put the condition that a<b<c, here's what i did so far:
Code:
#include <stdio.h>
int gcd(int m,int n)
{
  int r,s,x,y;
  if(m>=n){
    r=m-(m/n)*n;
    if(r==0)
      s=n;
  for(x=m,y=n;r!=0;x=y,y=r)
     {
       s=r;
       r=x-(x/y)*y;       
     }
    
  }
  if(n>m){
    r=n-(n/m)*m;
    if(r==0)
      s=m;
    for(x=m,y=n;r!=0;x=y,y=r)
     {
       s=r;
       r=x-(x/y)*y;      
     }
  }
return(s);
}
int pow(int a, int t)
{
  int q,p;
    for(p=1,q=1;q<=t;q++)
      p=a*p;

return(p);
}
main()
{
  int d,f=0,n,max,a,b,c;
 printf("insert the degree you wish to check for n-th pythogrean triplets\n");
 scanf("%d", &n);
 printf("insert the maximal number you want to check for\n");
 scanf("%d", &max);
 if(max>2){
   
   for(a=1;a<=max;a++)
     {
       for(b=2;b<=max;b++)
         {
   for(c=3;a<b,b<c,c<=max;c++)
     {
       if((pow(a,n)+pow(b,n)==pow(c,n))&& gcd(a,b)==1){
         f++;
         printf("the triplet is: %d, %d, %d \n", a,b,c);
       }
       if(pow(a,n)+pow(b,n)==pow(c,n)&& (gcd(a,b))!=1){
         f++;
          printf("the triplet is: %f, %f, %f \n", a/(gcd(a,b)),b/(gcd(a,b)),c/(gcd(a,b)));
       }
     }
         }
     }
 }

    
     

       if(max<=2)
        printf("there isn't such triplet \n");
 printf("the number of triplets in the specified domain is: %d \n", f);
 }
any hints where should i put this condition, i put it in the third loop, but it doesn't seem to work.
your help is appreciated.
 
here's an abbreviated code, without specifting the gcd and pow functions:
Code:
  int d,f=0,n,max,a,b,c;
 printf("insert the degree you wish to check for n-th pythogrean triplets\n");
 scanf("%d", &n);
 printf("insert the maximal number you want to check for\n");
 scanf("%d", &max);
 if(max>2){
   
   for(a=1;a<=max;a++)
     {
       for(b=2;b<=max;b++)
         {
   for(c=3;a<b,b<c,c<=max;c++)
     {
       if((pow(a,n)+pow(b,n)==pow(c,n))&& gcd(a,b)==1){
         f++;
         printf("the triplet is: %d, %d, %d \n", a,b,c);
       }
       if(pow(a,n)+pow(b,n)==pow(c,n)&& (gcd(a,b))!=1){
         f++;
          printf("the triplet is: %f, %f, %f \n", a/(gcd(a,b)),b/(gcd(a,b)),c/(gcd(a,b)));
       }
     }
         }
     }
 }

    
     

       if(max<=2)
        printf("there isn't such triplet \n");
 printf("the number of triplets in the specified domain is: %d \n", f);
 }
i hope someone can give me some hint.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
12
Views
2K
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K