Algorithm
Problem Name: 2 AD-HOC - beecrowd | 2116
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2116
Students Game
By Thiago Nepomuceno Brazil
Timelimit: 1
Juilherme and Jogerio, really like to play matemathical games. Juilherme create a new game to play while they watch this online contest.
The game use the following steps:
1) Juilherme choose a integer N and Jogerio choose a integer M.
2) Juilherme e Jogerio should then find two prime numbers P1 and P2, that should be the closest integer possible when compared with N and M, respectively. Besides that, P1 should be equal or less then N and P2 should be equal or less then M.
3) The final answer of the game is find the multiplication of P1 and P2. Who find the answer first will be the winner.
They will try to find the answer the fastest way possible, and sometimes they can make some mistake. So, they need a algorithm that delivers the final answer of the game, that will then be compared with their answer.
Using the information of the game, create a new program that give N and M print the final result of the game.
Input
The input will be only one line with N and M (2 <= N, M <= 1000)
Output
The output of the program should be only one line with the final answer of the game.
Input Samples | Output Samples |
10 15 |
91 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cmath>
#include <cstdio>
bool prime(int n) {
if (n == 2) {
return true;
}
if (n < = 1 || n % 2 == 0) {
return false;
}
for (int i = 3; i < = sqrt(n + 1) + 1; i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
int main() {
int x, y;
scanf("%d %d", &x, &y);
while (true) {
if (prime(x)) break;
x--;
}
while (true) {
if (prime(y)) break;
y--;
}
printf("%d\n", x * y);
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Python Programming
Code -
Python Programming
def ehprimo(n):
if n == 1 or n == 2: return True
t = int(n**0.5 + 2)
for i in range(2, t):
if n % i == 0: return False
return True
n, m = [int(x) for x in input().split()]
p1 = p2 = 0
while not p1:
if ehprimo(n): p1 = n
else: n -= 1
while not p2:
if ehprimo(m): p2 = m
else: m -= 1
print(p1*p2)
Copy The Code &
Try With Live Editor
Input
Output