Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

POTATOES - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Shalini Sah
Tester:Shiplu Hawlader and Mahbubul Hasan
Editorialist:Lalit Kundu

DIFFICULTY:

SIMPLE

PREREQUISITES:

Prime numbers

PROBLEM:

Given x(<=1000) and y(<=1000), find the smallest z(>0) such that (x+y+z) is prime.

EXPLANATION:

Keep increasing i (starting from 1) until (x+y+i) is not prime. i will never exceed 40 since x+y <= 2000. So we can afford to check each number one by one.
To check if a number N is prime:

def check_prime(N):      
    for i=2 to sqrt(N):     
        if N%i==0:     
            return false      
    return true

Alternatively, we can use sieve of eratosthenes to check primes.
Naive primality testing (checking if N is divisible by any number from 2 to N-1) could also pass if precomputation is done.

AUTHOR'S AND TESTER'S SOLUTIONS:

To be updated soon.


Viewing all articles
Browse latest Browse all 40121

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>