Thursday, September 13, 2018

NEAR PRIME

  • Problem Description

    You are on your way to find the gifts. All the gifts lie in your path in a straight line at prime numbers and your house is at 0.Given your current position find the closest gift to your position, and calculate the distance between your current position and gift and tell the distance.

    For the number= 0, the output is 2.
    The closest prime number to 0 is 2, so the answer is 2 - 0 = 2.

    For number = 11, the output should be 0.
    11 is a prime number, so the answer is 11 - 11 = 0

    For the number 16, the closest prime is 17
    So Output 17-16=1

    For the number 24, the closes prime is 29
    So Output 29-24=5

    Input/Output
    [time limit] 3000ms [input] string number
    Constraints: 0 int(number) 9 1014
  • CODING ARENA::
  • #include <stdio.h>
    int isprime(int n)
    {
      int i,f=0;
      for(i=2; i<=n/2; ++i)
        {
            if(n%i==0)
            {
                f=1;
                break;
            }
      }
      return f;
    }
    int main()
    {
      int a,i,max;
      scanf("%d",&a);
      for(i=a;i<100;i++)
      {
        if(isprime(i)==0)
        {max=i;
         break;
        }
      }
      printf("%d",max-a);
    return 0;
    }
  • Test Case 1

    Input (stdin)
    16
    
    
    Expected Output
    1
  • Test Case 2

    Input (stdin)
    24
    
    
    Expected Output
    5

1 comment: