Seven Thieves and Diamonds Riddle C Program

I recently wrote a C program for the following Seven Thieves and Diamonds puzzle:

"There are seven thieves, they steal diamonds from a diamond dealer and run away in the jungle. During the run, night falls and they decide to rest in the jungle. When they sleep every day, two of their best friends get up and decide to distribute diamonds among themselves and run away, so they start handing out, but discover that one diamond was superfluous. So they decided to wake up the third and divide the diamonds again ... only to their surprise they still find one extra diamond. decide to wake up the fourth. One more diamond again. 5- th woke up ...... one more extra, sixth one more extra. Now they wake up on the 7th and diamonds are distributed the same. "

Although the logic is pretty simple to understand, my program looks pretty bad. It only seems to work for numbers 3, 5 and 7.

I am new to programming in general and I feel like my program is not very complicated:

#include<stdio.h> 

int main() 
{ 
    int n,i,j,k; 
    int a[30]; 
    printf("Enter the 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 would be great if someone could help me develop this code into something more non-specific so that it can return output for all "n" values. Also, any feedback in general would be much appreciated.

+3


source to share


1 answer


Your code is very heavy, so I wrote my own code to debug this and your program, although unclear and difficult to follow, completely correct for correct input, you just don't handle all cases very well, so you are in a while loop forever. Not every input will work for this problem, only prime numbers will give you the answer to this problem, so inputs like 2, 4 and 6 won't work, so they will need to be processed.

Here is a test comparing your outputs to the test I wrote for valid inputs.

#Of Theives Your Code Test Code
    3 3 3
    5 25 25
    7 301 301
    11 25201 25201
    13 83161 83161

You can write a quick function to test this concern like this:

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

      



Then you can check for valid inputs that are greater than 1 (because then there won't be enough thieves for the story to happen) and run like this:

#include<stdio.h> 

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



int main() 
{ 
    int n,i,j,k; 
    int a[30]; 
    printf("Enter the number of thieves that is prime and greater than 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 I wrote to test the puzzle:

#include<stdio.h>


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

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

int main()
{
    int thieves, i;
    long diamonds, lcm = 1;
    printf("Enter the number of thieves that is prime and greater than 1:\n");
    scanf("%d",&thieves);

    if(isPrime(thieves) && thieves > 1)
    {
        for(i = 2;i < thieves;i++)
        {
                lcm = (lcm*i)/gcd(i,lcm);
        }

        i = 1;
        dimonds = lcm*i + 1;
        while(dimonds % thieves != 0)
        {
            dimonds = lcm*++i + 1;
        }
        printf("There are a minimum of diamonds is: %d\n",diamonds);
    }
    else 
    {
        printf("Input inv\n");

    }
}

      

+3


source







All Articles