#C8254. Largest Prime Finder

    ID: 52216 Type: Default 1000ms 256MiB

Largest Prime Finder

Largest Prime Finder

You are given an integer n. Your task is to find the largest prime number that is less than or equal to n. If n is not a positive integer greater than 1, then output Not valid.

Recall that a prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In mathematical notation, a number p is prime if:

\(p > 1\) and for every \(d \in \mathbb{N}\) with \(1 < d < p\), we have \(d \nmid p\).

For example:

  • For n = 10, the largest prime \(\leq 10\) is 7.
  • For n = 29, since 29 itself is prime, the answer is 29.
  • For edge cases like n = 1 or negative numbers, the output should be Not valid.

Write a program that reads the input from stdin and prints the result to stdout.

inputFormat

Input consists of a single integer n provided via standard input.

outputFormat

Output the largest prime number less than or equal to n if it exists; otherwise, output the string Not valid.## sample

10
7