Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Euclidean Algorithm for GCD and comparision with Brute force approach. #6030

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 99 additions & 0 deletions code/data_structures/Linked_lists/Header_Linked_List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//Part of Cosmos by OpenGenus Foundation
//Contributed by Tushar Gautam (Tushark21)

#include <iostream>
#include <conio.h>

using namespace std;

struct node{
int info;
node *next;
}*ptr,*head,*start;


//Function for creating new node and Inserting data in the node
struct node* insert(struct node* head,int data){
ptr=new node;
ptr->next=NULL;
ptr->info=data;

head->next=ptr;
cout<<"Data Inserted\n";
return ptr;
}

//Function for searching item in Linked List
int search(struct node* start,int item){
ptr=start->next;
int f,k;
f=0;
k=0;
while(ptr!=NULL){
k++;
if(ptr->info==item){
f++;
cout<<"Item Found at Node: "<<k<<"\n";
}
ptr=ptr->next;
}
return f;
}

//Function for Printing the Linked List
void printLL(struct node* start){

ptr=start->next;

while(ptr!=NULL){
cout<<ptr->info<<"->";
ptr=ptr->next;
}
cout<<"\n";

}

int main(){

int c=1,k=0,item,data;

ptr=new node;
ptr->next=NULL;
start=head=ptr;

while(c<4 && c>0){
cout<<"1.Insert\n2.Search Item\n3.Link List\n";
cin>>c;

switch(c){
case 1:
cout<<"Enter Data\n";
cin>>data;

head=insert(head,data);
break;

case 2:
cout<<"Enter Item that you want to find\n";
cin>>item;

int f;
f=search(start,item);
if(f==0){
cout<<"Item Not Found\n";
}
break;

case 3:
printLL(start);
break;

default:
cout<<"Wrong Choice\nExiting...\n";
}

}

getch();
return 0;
}
32 changes: 32 additions & 0 deletions code/mathematical-algorithms/Coprime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//Part of Cosmos by OpenGenus Foundation

import java.util.*;

public class coprime{

public static void main(String args[]){

Scanner i= new Scanner(System.in);
int a,b;
System.out.println("Enter 1st Number");
a=i.nextInt();
System.out.println("Enter 2nd Number");
b=i.nextInt();

while(b!=0) {
int x=a;
a=b;
b=x%b;
}

if(a==1){
System.out.println("Coprime");
}

else{
System.out.println("Not Coprime");
}
}


}
4 changes: 4 additions & 0 deletions code/mathematical-algorithms/euclidean_gcd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GCD-Greatest Common Divisor of two numbers is the largest number that divides both numbers with remainder zero.

Euclidean Algorithm:
Idea behind Euclidean's Algorithm is that GCD(a,b) is same as GCD(b%a,a).
63 changes: 63 additions & 0 deletions code/mathematical-algorithms/euclidean_gcd/euclidean_gcd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include<bits/stdc++.h>

using namespace std;

int steps=0;

int min(int x,int y){
if(x>y){
return x;
}
return y;
}

int gcd(int x,int y){
if(x==0 && y==0){
steps++;
return -1;
}

if(x==0 || y==0){
steps++;
return x==0?y:y;
}

int smallerNum=min(x,y),divisor;
for(int i=1;i<=smallerNum;i++){
if(x%i==0 && y%i==0){
divisor=i;
}
steps++;
}

return divisor;
}

int euclideanGCD(int x,int y){
steps++;
if(x==0 && y==0){
return -1;
}

if(x==0){
return y;
}

return euclideanGCD(y%x,x);
}

int main(){

int a,b;
cout<<"Enter 1st Number:";
cin>>a;
cout<<"Enter 2nd Number:";
cin>>b;

cout<<"GCD of "<<a<<" and "<<b<<" by Brute Force Approach is "<<gcd(a,b)<<"\n";
cout<<"Steps Taken by Brute Force Approach: "<<steps<<"\n";
steps=0;
cout<<"GCD of "<<a<<" and "<<b<<" by Euclidean's Algorithm is "<<euclideanGCD(a,b)<<"\n";
cout<<"Steps taken by Euclidean's Algorithm: "<<steps;
return 0;
}