Last active
August 29, 2015 14:10
-
-
Save swapnilshrikhande/39c86a9befe04a855fb8 to your computer and use it in GitHub Desktop.
Faulty machine problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int nextprime(int number); | |
int isprime(int value); | |
int main(void) | |
{ | |
// let size of each choc. be 50 grm | |
long int chocw=50; | |
long int expectedw=0; | |
long int resultw=0; | |
long int faultymachine; | |
long int faultysize; | |
long int totalmachines; | |
long int index=1; | |
long int lastnumber=0; | |
long int diff=0; | |
long int count=1; | |
printf("Enter Total Machines No. \n"); | |
scanf("%ld",&totalmachines); | |
printf("Enter weight of each choc \n"); | |
scanf("%ld",&chocw); | |
printf("Enter Faulty machine number \n"); | |
scanf("%ld",&faultymachine); | |
printf("Enter Faulty weight (should be less than < %d) \n",chocw); | |
scanf("%ld",&faultysize); | |
// Step 1 calculate weights | |
lastnumber=chocw; | |
for(index=1;index<=totalmachines;index++) { | |
lastnumber = nextprime(lastnumber); | |
expectedw += (lastnumber*chocw); | |
printf("M: %ld - %ld \n",index,lastnumber); | |
if(index == faultymachine){ | |
resultw += (lastnumber*faultysize); | |
}else{ | |
resultw += (lastnumber*chocw); | |
} | |
} | |
printf("Expected weight = %ld \n",expectedw); | |
printf("Actual weight = %ld \n",resultw); | |
diff = expectedw - resultw; | |
printf("Difference = %ld",diff); | |
// find prime factors of diff | |
// Solution | |
lastnumber = nextprime(chocw); | |
printf("\n\n-------thinking.......... \n"); | |
for(count=1;count<=totalmachines;count++) | |
{ | |
printf("M: %ld - %ld \n",count,lastnumber); | |
for(index=1;index<chocw;index++) | |
{ | |
if((index * lastnumber) == diff) | |
{ | |
printf("\n The Faulty machine is %ld \n",count); | |
} | |
} | |
lastnumber=nextprime(lastnumber); | |
} | |
getchar(); | |
return 1; | |
} | |
int nextprime(int number) | |
{ | |
while(1) | |
{ | |
++number; | |
if(isprime(number)) | |
{ | |
return number; | |
} | |
} | |
} | |
int isprime(int value){ | |
int i=2; | |
for(;i<=value/2;i++){ | |
if(value%i==0){ | |
return 0; | |
} | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment