What is the fastest way to find the GCD of two numbers? - c

I have an array of size n. I need to find the GCD of each element with a given number and if it's greater than 1, add it to another array. What's the fastest way to do this?

int gcd(int a, int b)
{
if(b == 0) {
return a;
}
else {
return gcd(b, a % b);
}
}

For small numbers use binary GCD (which is faster Euclid's GCD algorithm) & for large numbers try Lehmer's algorithm.
Binary GCD: https://www.geeksforgeeks.org/steins-algorithm-for-finding-gcd/

The following code uses the normal method that we humans use to calculate the GCD and is by far, according to me the fastest way to find GCD(HCF) of 2 numbers:
#include <iostream>
using namespace std;
int main()
{
int N1;
int N2;
cin<<N1;
cin<<N2;
int small=N1;
int big=N2;
if (N1>N2){
N1=small;
N2=big;
}
int P=1;
int i=1;
while (i<=N1){
if (N1%i==0 && N2%i==0){
N1=N1/i;
N2=N2/i;
P=P*i;
i=1;
}
i=i+1;
}
cout<<"GCD= "<<P;
return 0;
}

#include<stdio.h>
int main()
{
int a,b,a1,b1;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
a1=a;b1=b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
printf("GCD of %d and %d is %d\n",a1,b1,a);
printf("LCM of %d and %d is %d",a1,b1,(a1*b1/a));
}

Related

Using a recursive function ,find a program that computes the multiplication of two numbers using addition operator

Using a recurrence function ,find a program that computes the multiplication of two numbers using addition operator.
What I found is as follows:
/*C program to multiply two numbers using plus operator.*/
#include <stdio.h>
int main()
{
int a,b;
int mul,loop;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
mul=0;
for(loop=1;loop<=b;loop++){
mul += a;
}
printf("Multiplication of %d and %d is: %d\n",a,b,mul);
return 0;
}
However I'm not sure if the it uses a recurrence function,can someone check that and if it does use a recursive function then show me how to do that?
This simple logic should work for you:
int multiply(int a, int b)
{
if(a < b)
return multiply(b, a); // swap
else if(b != 0)
return (a + multiply(a, b - 1)); // recursion
else
return 0;
}

What is a recursive function for this program I coded for adding all the digits of a five digit number

I'm not able to code a recursive function for this, I have given the example after the program,
#include<stdio.h>
int sum(int x);
int main()
{
int n,s;
printf("enter the five digit number whose digits need to be added");
scanf("%d",&n);
s= sum(n);
printf("The sum of all the digits of a five digit number is %d",s);
}
int sum(int x)
{
int d=0,a;
for(i=1;i>=5;i++)
{
a=x%10;
x=x/10;
d= d+a;
}
return(d);
}
The following is the recursive code for the above program that I coded myself,
#include<stdio.h>
int sum(int x);
int main()
{
int n,s;
printf("enter the five digit number whose digits need to be added\n");
scanf("%d",&n);
s= sum(n);
printf("The sum of all the digits of a five digit number %d",s);
}//This is my poor try inspired by coderedoc
//please fix this code my laptop battery gonna be over already
int sum(int x)
{
int d=0, a;
if(x<0)
return sum(-x);
else
a= x%10;
x= x/10;
d=d+a;
return sum(x);
else
if(x==0)
return d;
}
int sum(int x){
if( x < 0) return sum(-x);
return (x==0)?0:(x%10)+sum(x/10);
}
The code will be as simple as this. If you reach the x=0 state you are done. Else add the last digit with the sum of the rest of the digits.
Also when building your solution - try to make it generalize to some extent. 5 digit is good but think if you can extend it for more numbers of digits. This works for that too.
int sum(int x){
if(x < 0) return sum(-x);
if(x == 0)
return 0;
else
return (x%10)+sum(x/10);
}

C program on part of algorithm which focus on finding Armstrong-like number

I have a problem with C program. The idea of it is similar to Armstrong number checking. Say if the input number is 123. Program needs to check if condition, for example 123=1^1+2^2+3^3 is true. I know how to add digits,but have a problem with powers. It is obvious that I need a loop for powers from 1 to the number of digits. In Armstrong number algorithm you have similar power on every digit. For example 153=1^3+5^3+3^3. Here is what I have so far:
#include<stdio.h>
int main()
{
int n,d,s=0,o,i,k;
printf("n=");scanf("%d",&n);
d=n;
while(d!=0)
{
o=d%10;
s=s+o;
d=d/10;
k++
}
printf("sum:%d",s);
printf("number of digits:%d",k);
return 0;
}
Thanks for the answers.
You need first get the lenth of number, which is used to determine how many times you need to get into loop to calculate each bit.
For example, number 123, you first need to know the number is 3 bits len, then you can mutilply number 3 three times, number 2 twice, and number 1 once.
I use a temporary string to achieve this
here is codeļ¼Œ a little bit alteration on yours
#include <stdio.h>
#include <string.h>
#define MAX_NUM_LEN 16
int main()
{
char tmp_num[MAX_NUM_LEN] = {0};
int len,n,d,s=0,o,i,tmp_len, tmp_o;
printf("n=");scanf("%d",&n);
sprintf(tmp_num, "%d", n);
len = strlen(tmp_num);
tmp_len = len;
d=n;
while(d!=0)
{
o=d%10;
for (tmp_o = 1, i = tmp_len; i > 0; i--)
tmp_o *= o;
s=s+tmp_o;
d=d/10;
tmp_len--;
}
printf("sum:%d\n",s);
printf("number of digits:%d\n",len);
return 0;
}
results:
According of what I've understood I think this is what the OP is looking for:
int power(int base, int exp)
{
if (base == 0) return 0;
int result=1;
while (exp-- > 0) result*=base;
return result;
}
void calculate(int number)
{
int d=number;
int tmpnumber=number;
int n=0;
while (d > 0)
{
n++;
d /=10;
}
printf("Number of digits: %d\n", n);
int k=0;
int sum=0;
while (n--)
{
// get digits from left to right
d=number / power(10, n);
k++;
sum+=power(d, k);
number %= power(10, n);
printf("%d^%d=%d\n", d, k, power(d, k));
}
printf("\n%5d %5d", tmpnumber, sum);
}
int main(int argc,char *argv[])
{
int value;
while (TRUE)
{
printf("Enter value (0 = Quit): ");
scanf("%d", &value);
if (value <= 0) return 0;
calculate(value);
printf("\n");
}
}

Intersection of 2 vectors, logical error

I want to write a code (in c) that prints the intersection of two vectors of "N" lenght and cannot print the same number twice. The vectors will be filled with a function, that reads the input ( any number), one by one. And after all, the code needs to print another vector with the intersection between the first two vectors. Until now i wrote the code below, but it has a logical error that i cant solve.
#include <stdlib.h>
#include <stdio.h>
void preencheVetor(int* v, int tamanho){
int i=0;
for(i=0;i<tamanho;i++){
scanf("%d", *(v+i));
printf("\n");
}
}
void interVetor(int* v1, int* v2, int tamanho){
int* v3=malloc(sizeof(int)*tamanho);
int i1=0;
int i2=0;
int i3=0;
int c=0;
for(i1=0;i1<tamanho;i1++){
for(i2=0;i2<tamanho;i2++){
if((v1[i1])==(v2[i2])){
*(v3+c)=v2[i2];
c++;
}
}
}
for(i3=0;i3<tamanho;i3++){
printf("%d", *(v3+i3));
printf("\n");
}
}
int main(){
int n=0;
printf("Vectors Lenght:\n");
scanf("%d", &n);
printf("\n\n");
int v1[n];
int v2[n];
preencheVetor(v1, n);
printf("\n\n");
preencheVetor(v2, n);
printf("\n\n");
interVetor(v1, v2, n);
system("PAUSE");
return 0;
}
The size of vector v3 is c not tamanho. Fix the for(i3...) loop.
Other than that, I think your intersection works OK.
It is very inefficient, of course, if the vectors are not sorted.

GCD of two numbers using Recursive function - What's Wrong?

I am new to Programming, just a student.
I have written a program to calculate the GCD of two numbers using recursive function, but it's giving the correct answer for some, while it gives wrong answer for a few others. Kindly help me in identifying the problem:
#include<stdio.h>
#include<conio.h>
int gcd(int,int,int)
int main(){
int a,b,x,val;
printf("Enter the first number: ");
scanf("%d",&a);
printf("Enter the second number: ");
scanf("%d",&b);
if(a>b)
x=b;
else
x=a;
val=gcd(a,b,x);
printf("The GCD of the two numbers you entered is:%d",val);
getch();
return 0;
}
int gcd(int a,int b,int x){
if(a%x==0){
if (b%x==0)
return x;
}else
return gcd(a,b,x-1);
}
For example, the program gives a wrong answer when first number = 69, second number = 65, whereas in some other cases it mysteriously gives the right answer.
Can someone help me out here?
Check your code path. Not all condition return an integer in the gcd function.
Which compiler are you using? It should give you a warning or error.
Try this:
int gcd(int a,int b,int x){
if(a%x==0 && b%x==0) return x;
}else return gcd(a,b,x-1);
}
This catches both the modulus comparisons to 0 in one if statement, all conditions not falling within this get gcd calls.
Example: consider the numbers a=100 and b=44. When it reaches x=25, which divides 100, but not 44, your code doesn't have a proper path to take.
You are not taking all the conditions (paths) into consideration.
int gcd(int a,int b,int x){
if(a%x==0){
if (b%x==0)
return x;
else
// ERROR ERROR ERROR,
// if the code reaches here, then it neither calls gcd recursively,
// nor does it return anything valueable
}else
return gcd(a,b,x-1);
}
You should change the code as below.
int gcd(int a,int b,int x){
if(a%x==0)
if (b%x==0)
{
return x;
}
return gcd(a,b,x-1);
}
OR
int gcd(int a,int b,int x){
if(a%x==0 && b%x==0) return x;
return gcd(a,b,x-1);
}
GCD of two numbers in C (the easiest way):
while(secNum != 0) {
Temp = fNum % secNum;
fNum = secNum;
secNum = Temp;
}
printf("GCD of the given two numbers : %d",fNum);

Resources