You are on page 1of 25

1

Program to calculate 100! #include #include #include #include <assert.h> <stdio.h> <stdlib.h> <gmp.h>

void factorial(mpz_t result, unsigned long input) { mpz_set_ui(result, 1); while (input > 1) { mpz_mul_ui(result, result, input--); } } int main() { mpz_t fact; unsigned long input = 0; char *buf; mpz_init(fact); scanf("%lu", &input); factorial(fact, input); buf = malloc(mpz_sizeinbase(fact, 10) + 1); assert(buf); mpz_get_str(buf, 10, fact); printf("%s\n", buf); free(buf); mpz_clear(fact); }

2
Kruskals Algorithm for Minimum Spanning Tree
Posted by Vinod on October 5, 2006

1 Votes

/************************************************************ -> This Program is to implement Kruskal algorithm. -> This program is to find minimum spanning tree for undirected weighted graphs -> Data Structers used: Graph:Adjacency Matrix -> This program works in microsoft vc++ 6.0 environment. **************************************************************/ #include<iostream.h> class kruskal { private: int n; //no of nodes int noe; //no edges in the graph int graph_edge[100][4]; int tree[10][10]; int sets[100][10]; int top[100]; public: void read_graph(); void initialize_span_t(); void sort_edges(); void algorithm();

int find_node(int ); void print_min_span_t(); }; void kruskal::read_graph() { cout<<*************************************************\n <<This program implements the kruskal algorithm\n <<*************************************************\n; cout<<Enter the no. of nodes in the undirected weighted graph ::; cin>>n; noe=0; cout<<Enter the weights for the following edges ::\n; for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { cout<< < <<i<< , <<j<< > ::; int w; cin>>w; if(w!=0) { noe++; graph_edge[noe][1]=i; graph_edge[noe][2]=j; graph_edge[noe][3]=w; } } } // print the graph edges cout<<\n\nThe edges in the given graph are::\n; for(i=1;i<=noe;i++) cout<< < <<graph_edge[i][1] << , <<graph_edge[i][2] << > ::<<graph_edge[i][3]<<endl;

} void kruskal::sort_edges() { /**** Sort the edges using bubble sort in increasing order**************/ for(int i=1;i<=noe-1;i++) { for(int j=1;j<=noe-i;j++) { if(graph_edge[j][3]>graph_edge[j+1][3]) { int t=graph_edge[j][1]; graph_edge[j][1]=graph_edge[j+1][1]; graph_edge[j+1][1]=t; t=graph_edge[j][2]; graph_edge[j][2]=graph_edge[j+1][2]; graph_edge[j+1][2]=t; t=graph_edge[j][3]; graph_edge[j][3]=graph_edge[j+1][3]; graph_edge[j+1][3]=t; } } } // print the graph edges cout<<\n\nAfter sorting the edges in the given graph are::\n; for(i=1;i<=noe;i++) cout<< < <<graph_edge[i][1] << , <<graph_edge[i][2] << > ::<<graph_edge[i][3]<<endl; } void kruskal::algorithm() { // ->make a set for each node for(int i=1;i<=n;i++)

{ sets[i][1]=i; top[i]=1; } cout<<\nThe algorithm starts ::\n\n; for(i=1;i<=noe;i++) { int p1=find_node(graph_edge[i][1]); int p2=find_node(graph_edge[i][2]); if(p1!=p2) { cout<<The edge included in the tree is :: << < <<graph_edge[i][1]<< , <<graph_edge[i][2]<< > <<endl<<endl; tree[graph_edge[i][1]][graph_edge[i][2]]=graph_edge[i][3]; tree[graph_edge[i][2]][graph_edge[i][1]]=graph_edge[i][3]; // Mix the two sets for(int j=1;j<=top[p2];j++) { top[p1]++; sets[p1][top[p1]]=sets[p2][j]; } top[p2]=0; } else { cout<<Inclusion of the edge << < <<graph_edge[i][1]<< , <<graph_edge[i][2]<< > <<forms a cycle so it is removed\n\n; } } }

int kruskal::find_node(int n) { for(int i=1;i<=noe;i++) { for(int j=1;j<=top[i];j++) { if(n==sets[i][j]) return i; } } return -1; } int main() { kruskal obj; obj.read_graph(); obj.sort_edges(); obj.algorithm(); return 0; }

3
Program To Implement Tower Of Hanoi Alogithm Using Recursion
by nehawalia Tue Mar 29, 2011 9:08 pm

Program To Implement Tower Of Hanoi Alogithm Using Recursion. This program shows the movements of disk from one tower to another when a key is pressed. Code :

#include<iostream.h> #include<stdio.h> #include<conio.h> class tower { int *t1,*t2,*t3; int x,y,z; public: void disp_tower(); void move_disk(int tx,int ty); void toh(int n,int a,int b,int c); tower(int no); ~tower(); }; tower :: tower(int no) { t1 = new int[no+1]; t2 = new int[no+1]; t3 = new int[no+1]; x = no; y = z = 0; for(int i=0,j=no ; i<no ; i++,j--) { t1[i] = j; t2[i] = t2[i] = 0; }

t1[no] = t2[no] = t3[no] = 0; } tower :: ~tower() { delete []t1; delete []t2; delete []t3; } void tower :: disp_tower() { clrscr(); cout<<" X :: "; for(int i=0;i<x;i++) { cout<<" "<<t1[i]; } cout<<" Y :: "; for(i=0;i<y;i++) { cout<<" "<<t2[i]; } cout<<" Z :: "; for(i=0;i<z;i++) { cout<<" "<<t3[i]; } getch(); } void tower :: toh(int n,int tx,int ty,int tz) //x to y using z { if(n>=1)

{ toh(n-1,tx,tz,ty); move_disk(tx,ty); //x to y disp_tower(); toh(n-1,tz,ty,tx); } } void tower :: move_disk(int tx,int ty) { switch(tx) { case 1: { if(ty==2) t2[y++] = t1[--x]; else t3[z++] = t1[--x]; }break; case 2: { if(ty==1) t1[x++] = t2[--y]; else t3[z++] = t2[--y]; }break; case 3: { if(ty==1) t1[x++] = t3[--z]; else t2[y++] = t3[--z]; }break; }//end of switch } //-------------------------------------------------------------------------int main(void) { clrscr();

cout<<"Enter the no. of disks::"; int no; cin>>no; tower obj(no); obj.disp_tower(); obj.toh(no,1,2,3); getch(); return 0; } //--------------------------------------------------------------------------

4
Program to multiply two single variablepolynomials using linked lists. Author: Murugan Ramaswamy */ #include<iostream.h> #include<conio.h> #include<stdlib.h> class POLY { int Coeff,Pow; POLY *Next; public: friend POLY *Create(); friend POLY *MUL(POLY *, POLY *); friend POLY *Insert(int, int, POLY *); friend POLY *SORT(POLY *); friend void Display(POLY *); }; POLY *SORT(POLY *P) { POLY *Temp1,*Temp2; for(Temp1=P;Temp1;Temp1=Temp1->Next) { for(Temp2=Temp1->Next;Temp2;Temp2=Temp2->Next) { if((Temp1->Pow==Temp2->Pow)&&(Temp1->Pow!=-1)) { Temp1->Coeff=Temp1->Coeff+Temp2->Coeff; Temp2->Coeff=Temp2->Pow=-1; } } } free(Temp1); free(Temp2); Temp2=Temp1; for(;P;P=P->Next) { if(P->Pow!=-1) Temp1=Insert(P->Coeff,P->Pow,Temp1); } Temp1->Next=NULL; P=Temp2->Next; free(Temp1); free(Temp2); return P; } POLY *MUL(POLY *P1,POLY *P2) { POLY *END, *Rear, *Temp; Rear=END=NULL; END=Rear; int TotalC,TotalP;

for(;P1;P1=P1->Next) { for(Temp=P2;Temp;Temp=Temp->Next) { TotalC=P1->Coeff*Temp->Coeff; TotalP=P1->Pow+Temp->Pow; Rear=Insert(TotalC,TotalP,Rear); } } Rear->Next=NULL; Rear=END->Next; free(END); Rear=SORT(Rear); return Rear; } void Display(POLY *P) { while(P!=NULL) { if(P->Coeff!=0) cout<<P->Coeff<<"(X^"<<P->Pow<<") "; P=P->Next; } } POLY *Create() { POLY *Rear, *Temp, *Poly; Poly=Rear=NULL; int C,P; while(1) { cout<<"\n > Enter Coefficient And Exponent : "; cin>>C>>P; Temp=new POLY; Temp->Coeff=C; Temp->Pow=P;Temp->Next=NULL; if(Poly==NULL) Poly=Rear=Temp; else { Rear->Next=Temp; Rear=Temp; } if(P==0) break; } return Poly; } POLY *Insert(int C, int P, POLY *Temp) { Temp->Next=new POLY;Temp=Temp->Next; Temp->Coeff=C; Temp->Pow=P; return Temp; } void main() {

clrscr(); POLY *Poly1, *Poly2, *Poly3; int i; cout<<"\nNOTE: ENTER THE COEFFICIENTS AND EXPONENTS IN THE"; cout<<"\n DESCENDING ORDER OF COEFFICIENTS."; cout<<"\n Put 0 As Exponent To Exit\n\n"; for(i=0;i<80;i++) cout<<"";cout<<"\n\n"; cout<<" > Enter The First Polynomial....\n\n"; Poly1=Create(); cout<<"\n\n >The First Expression Is\n\n > "; Display(Poly1); cout<<"\n\n" ; for(i=0;i<80;i++) cout<<"";cout<<"\n\n"; cout<<" > Enter The Second Polynomial....\n\n"; Poly2=Create(); cout<<"\n\n >The Second Expression Is\n\n > "; Display(Poly2); cout<<"\n\n"; for(i=0;i<80;i++) cout<<"";cout<<"\n\n"; cout<<" >The Result Is\n\n > "; Poly3=MUL(Poly1,Poly2); Display(Poly3); cout<<"\n\n"; for(i=0;i<80;i++) cout<<""; getch();

5
WAP to find the shortest path between two nodes in a graph using Dijkstra algorithm.

class Dijkstra {

private int rank = 0; private int[,] L; private int[] C; public int[] D; private int trank = 0; public Dijkstra(int paramRank,int [,]paramArray) { L = new int[paramRank, paramRank]; C = new int[paramRank]; D = new int[paramRank]; rank = paramRank; for (int i = 0; i < rank; i++) { for (int j = 0; j < rank; j++) { L[i, j] = paramArray[i, j]; } } for (int i { C[i] = } C[0] = -1; for (int i D[i] = = 0; i < rank; i++) i; = 1; i < rank; i++) L[0, i];

} public void DijkstraSolving() { int minValue = Int32.MaxValue; int minNode = 0; for (int i = 0; i < rank; i++) { if (C[i] == -1) continue; if (D[i] > 0 && D[i] < minValue) { minValue = D[i]; minNode = i; } } C[minNode] = -1; for (int i = 0; i < rank; i++) { if (L[minNode, i] < 0) continue; if (D[i] < 0) { D[i] = minValue + L[minNode, i]; continue; } if ((D[minNode] + L[minNode, i]) < D[i]) D[i] = minValue+ L[minNode, i]; } } public void Run() { for (trank = 1; trank >rank; trank++) { DijkstraSolving(); Console.WriteLine("iteration" + trank); for (int i = 0; i < rank; i++) Console.Write(D[i] + " "); Console.WriteLine(""); for (int i = 0; i < rank; i++) Console.Write(C[i] + " "); Console.WriteLine(""); }

} }

6
WAP to demonstrate inorder, preorder and postorder traversal in a binary search tree.

//Binary Search Tree Program #include <iostream> #include <cstdlib> using namespace std; class BinarySearchTree { private: struct tree_node { tree_node* left; tree_node* right; int data; }; tree_node* root; public: BinarySearchTree() { root = NULL; } bool void void void void void void void void }; // Smaller elements go left // larger elements go right void BinarySearchTree::insert(int d) { tree_node* t = new tree_node; tree_node* parent; t->data = d; t->left = NULL; t->right = NULL; parent = NULL; // is this a new tree? if(isEmpty()) root = t; else { //Note: ALL insertions are as leaf nodes tree_node* curr; curr = root; // Find the Node's parent isEmpty() const { return root==NULL; } print_inorder(); inorder(tree_node*); print_preorder(); preorder(tree_node*); print_postorder(); postorder(tree_node*); insert(int); remove(int);

while(curr) { parent = curr; if(t->data > curr->data) curr = curr->right; else curr = curr->left; } if(t->data < parent->data) parent->left = t; else parent->right = t; } } void BinarySearchTree::remove(int d) { //Locate the element bool found = false; if(isEmpty()) { cout<<" This Tree is empty! "<<endl; return; } tree_node* curr; tree_node* parent; curr = root; while(curr != NULL) { if(curr->data == d) { found = true; break; } else { parent = curr; if(d>curr->data) curr = curr->right; else curr = curr->left; } } if(!found) { cout<<" Data not found! "<<endl; return; } // 3 cases // 1. We're removing a // 2. We're removing a // 3. we're removing a : leaf node node with a single child node with 2 children

// Node with single child if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL && curr->right == NULL)) { if(curr->left == NULL && curr->right != NULL)

{ if(parent->left == curr) { parent->left = curr->right; delete curr; } else { parent->right = curr->right; delete curr; } } else // left child present, no right child { if(parent->left == curr) { parent->left = curr->left; delete curr; } else { parent->right = curr->left; delete curr; } } return; } //We're looking at a leaf node if( curr->left == NULL && curr->right == NULL) { if(parent->left == curr) parent->left = NULL; else parent->right = NULL; delete curr; return; } //Node with 2 children // replace node with smallest value in right subtree if (curr->left != NULL && curr->right != NULL) { tree_node* chkr; chkr = curr->right; if((chkr->left == NULL) && (chkr->right == NULL)) { curr = chkr; delete chkr; curr->right = NULL; } else // right child has children { //if the node's right child has a left child // Move all the way down left to locate smallest element if((curr->right)->left != NULL) { tree_node* lcurr; tree_node* lcurrp;

lcurrp = curr->right; lcurr = (curr->right)->left; while(lcurr->left != NULL) { lcurrp = lcurr; lcurr = lcurr->left; } curr->data = lcurr->data; delete lcurr; lcurrp->left = NULL; } else { tree_node* tmp; tmp = curr->right; curr->data = tmp->data; curr->right = tmp->right; delete tmp; } } return; } } void BinarySearchTree::print_inorder() { inorder(root); } void BinarySearchTree::inorder(tree_node* p) { if(p != NULL) { if(p->left) inorder(p->left); cout<<" "<<p->data<<" "; if(p->right) inorder(p->right); } else return; } void BinarySearchTree::print_preorder() { preorder(root); } void BinarySearchTree::preorder(tree_node* p) { if(p != NULL) { cout<<" "<<p->data<<" "; if(p->left) preorder(p->left); if(p->right) preorder(p->right); } else return; } void BinarySearchTree::print_postorder()

{ postorder(root); } void BinarySearchTree::postorder(tree_node* p) { if(p != NULL) { if(p->left) postorder(p->left); if(p->right) postorder(p->right); cout<<" "<<p->data<<" "; } else return; } int main() { BinarySearchTree b; int ch,tmp,tmp1; while(1) { cout<<endl<<endl; cout<<" Binary Search Tree Operations "<<endl; cout<<" ----------------------------- "<<endl; cout<<" 1. Insertion/Creation "<<endl; cout<<" 2. In-Order Traversal "<<endl; cout<<" 3. Pre-Order Traversal "<<endl; cout<<" 4. Post-Order Traversal "<<endl; cout<<" 5. Removal "<<endl; cout<<" 6. Exit "<<endl; cout<<" Enter your choice : "; cin>>ch; switch(ch) { case 1 : cout<<" Enter Number to be inserted : "; cin>>tmp; b.insert(tmp); break; case 2 : cout<<endl; cout<<" In-Order Traversal "<<endl; cout<<" -------------------"<<endl; b.print_inorder(); break; case 3 : cout<<endl; cout<<" Pre-Order Traversal "<<endl; cout<<" -------------------"<<endl; b.print_preorder(); break; case 4 : cout<<endl; cout<<" Post-Order Traversal "<<endl; cout<<" --------------------"<<endl; b.print_postorder(); break; case 5 : cout<<" Enter data to be deleted : "; cin>>tmp1; b.remove(tmp1); break; case 6 : return 0;

} } }

7
WAP to implement heap sort algorithm

#include <iostream.h> const int MAX = 10 ; class array { private : int arr[MAX] ; int count ; public : array( ) ; void add ( int num ) ; void makeheap(int ) ; void heapsort( ) ; void display( ) ; } ; array :: array( ) { count = 0 ; for ( int i = 0 ; i < MAX ; i++ ) arr[MAX] = 0 ; } void array :: add ( int num ) { if ( count < MAX ) { arr[count] = num ; count++ ; } else cout << "\nArray is full" << endl ; } void array :: makeheap(int c) { for ( int i = 1 { int val int s = int f = while ( { ; i < c ; i++ ) = i ( s arr[i] ; ; s - 1 ) / 2 ; > 0 && arr[f] < val )

arr[s] = arr[f] ; s = f ; f = ( s - 1 ) / 2 ; } arr[s] = val ; } } void array :: heapsort( ) { for ( int i = count - 1 ; i > 0 ; i-- ) { int ivalue = arr[i] ; arr[i] = arr[0] ; arr[0]=ivalue; makeheap(i); } } void array :: display( )

{ for ( int i = 0 ; i < count ; i++ ) cout << arr[i] << "\t" ; cout << endl ; } void main( ) { array a ; a.add ( 11 ) ; a.add ( 2 ) ; a.add ( 9 ) ; a.add ( 13 ) ; a.add ( 57 ) ; a.add ( 25 ) ; a.add ( 17 ) ; a.add ( 1 ) ; a.add ( 90 ) ; a.add ( 3 ) ; a.makeheap(10) ; cout << "\nHeap Sort.\n" ; cout << "\nBefore Sorting:\n" ; a.display( ) ; a.heapsort( ) ; cout << "\nAfter Sorting:\n" ; a.display( ) ; }

8
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

void prob1() { int s = 0; for(int i = 1; i < 1000; i++) { if(i % 3 == 0 || i % 5 == 0) s += i; } Stdout(s).newline; } void prob2() { auto target= 600_851_475_143; for(ulong i = 3; i * i < target; i++) if(target % i == 0) { Stdout(i).newline; return; } Stdout(target)(" is prime!").newline; } void prob3() { for(int i = 1; i < 1000; i++) for(int j = i; i + j < 1000; j++) { int k = 1000 - i - j; if(k < j) break; if(i * i + j * j == k * k) { Stdout(i, j, k).newline; } } } void main() { prob1; prob2;

prob3; } Answers: 233168 71 200, 375, 425

You might also like