Sum of Primes
InterviewTime limit15sMemory limit1024 MB
Given N, find a contiguous run of consecutive primes whose sum is exactly N, or print NIE if no such run exists.
- Level
Medium5 of 10
- Topics
- Two pointers, Sliding window, Math
- Solved
- No attempts yet
Statement
A natural number is called prime if it has exactly two distinct divisors, and . For example, is not prime because divides it, and is not prime because its only divisor is . The numbers and are prime.
Problem
Bajtazar loves prime numbers. He wrote consecutive primes on a sheet of paper: He wants to choose a contiguous segment of this sequence whose sum is exactly his favorite number . Write a program that, given , finds any contiguous interval of the prime sequence whose sum is exactly .
Input
The first and only line of input contains one natural number (), the sum Bajtazar wants.
Output
The first and only line of output should contain two primes and () such that the sum of the primes in the closed interval is exactly . If there are several solutions, your program may output any of them. If no solution exists, output only the single word NIE.