puzzle - Seven thieves & diamonds riddle C program -


i wrote c program following 'seven thieves , diamonds' puzzle:

"there 7 thieves, steal diamonds diamond merchant , run away in jungle. while running, night sets in , decide rest in jungle when everybody’s sleeping, 2 of best friends , decide distribute diamonds among , run away. start distributing find 1 diamond extra. decide wake 3rd 1 , divide diamonds again …..only surprise still find 1 diamond extra. decide wake fourth one. again 1 diamond spare. 5th woken up……still 1 extra. 6th still 1 extra. wake 7th , diamonds distributed equally."

although logic quite simple understand, program seems quite buggy. seems run numbers 3, 5 , 7.

i new programming in general , feel program not sophisticated:

#include<stdio.h>   int main()  {      int n,i,j,k;      int a[30];      printf("enter number of thieves\n");      scanf("%d",&n);      i=n+1;      while(1)      {          j=2;          k=0;          while(j<n)          {              if(i%j == 1 && i%n==0)              {                  a[k]=1;              }              else              {                  a[k]=0;              }              if(k==n-2)              {                  k=0;              }              j++;              k++;          }          for(j=0;j<n-1;j++)          {              if(a[j]==0)              {                  break;              }              else if(j==n-3 && a[j] == 1)              {                  printf("the number of diamonds = %d\n",i);                  return;              }          }          i++;      }  }  

it great if me develop code more nonspecific, such return output values of 'n.' also, feedback in general highly appreciated.

your code hard follow, wrote own code debug , program although obscure , hard follow correct valid inputs, not handling cases in while loop forever. not every input work problem prime numbers give answer problem inputs 2, 4, , 6 not work need handled.

here test comparing outputs test wrote valid inputs.

 #of theives   code    test code     3            3            3     5            25           25     7            301          301     11           25201        25201     13           83161        83161 

you can write quick function test care of this:

int isprime(int tmp) {     int i;     for(i = 2; <= tmp/2; i++)     {         if(tmp % == 0)             return 0;     }     return 1; } 

then can check valid inputs numbers greater 1 (because there not enough thiefs story happen) , prime so:

#include<stdio.h>   int isprime(int tmp) {     int i;     for(i = 2; <= tmp/2; i++)     {         if(tmp % == 0)             return 0;     }     return 1; }    int main()  {      int n,i,j,k;      int a[30];      printf("enter number of thieves prime , greater 1\n");      scanf("%d",&n);      i=n+1;     if(isprime(n) && n > 1)     {         while(1)          {              j=2;              k=0;              while(j<n)              {                  if(i%j == 1 && i%n==0)                  {                      a[k]=1;                  }                  else                  {                      a[k]=0;                  }                  if(k==n-2)                  {                      k=0;                  }                  j++;                  k++;              }              for(j=0;j<n-1;j++)              {                  if(a[j]==0)                  {                      break;                  }                  else if(j==n-3 && a[j] == 1)                  {                      printf("the number of diamonds = %d\n",i);                      return;                  }              }              i++;          }      }     else     {     printf("input invalid.\n");      } }  

the code wrote test riddle:

#include<stdio.h>   int isprime(int tmp) {     int i;     for(i = 2; <= tmp/2; i++)     {         if(tmp % == 0)             return 0;     }     return 1; }  long gcd(long a, long b) {   if (b == 0) {     return a;   }   else {     return gcd(b, % b);   } }  int main() {     int thieves, i;     long diamonds, lcm = 1;     printf("enter number of thieves prime , greater 1:\n");     scanf("%d",&thieves);      if(isprime(thieves) && thieves > 1)     {         for(i = 2;i < thieves;i++)         {                 lcm = (lcm*i)/gcd(i,lcm);         }          = 1;         dimonds = lcm*i + 1;         while(dimonds % thieves != 0)         {             dimonds = lcm*++i + 1;         }         printf("there minimum of diamonds is: %d\n",diamonds);     }     else      {         printf("input inv\n");      } } 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -