NEARPRIME - Số Nguyên Tố Gần Nhất - Quan, Tran Hoang

Skip to main content

NEARPRIME - Số nguyên tố gần nhất

Đề bài: Cho số nguyên N (1 <= N <= 2 * 10^6) nhập từ bàn phím. Hãy đưa ra số M là số nguyên tố gần với N nhất. Số nguyên tố gần với N được định nghĩa là một số nguyên tố mà khoảng cách từ N tới nó là nhỏ nhất. Nếu ta tìm được 2 số nguyên tố có cùng khoảng cách tới N, ưu tiên in ra màn hình số nhỏ hơn (codelearn.io - practice easy). Testcase #1: Input: 51 Output: 53 Testcase #2: Input: 1000 Output: 997 Solution: Dựng sàng Eratosthenes, sau đó tìm số nguyên tố gần nhất bên trái và bên phải. Nếu khoảng cách đến số nguyên tố bên trái lớn hơn khoảng cách đến số bên phải thì in số bên phải ra, còn lại in số bên trái. Code:

Comments

Post a Comment

Popular Posts

Image

Install & configure Atom for C/C++ learning.

(Written in English, since I've just got Grammarly installed and curious to see how it works).  Since I came to college, many friends have asked me: "what IDE is that? looks so cool/nice/awesome, how can I install it?". So I decided to write this small (but to be honest, not short) tutorial, digging in how to install Atom text-editor, customise and install further extensions for C/C++ learning.  Note that this is not a complete, detailed tutorial. I just give you some YouTube and other pages link, then you'll do steps yourself since other tutorials are more detailed than what I can write.  Let's get started.  The reason why I choose Atom  Atom installation is quite simple, but the configuration to make it supports C/C++ is some kind of complex itself.  I choose Atom in a coincidence. Before learning C++, I was a PHP/JavaScript developer. At that time, I used Sublime Text 3 (of course, without a license). Then, I'... Image

Email Aliases with Gmail and Mail.ru

Email, again.

Năm 2024

Mở đầu bằng mẩu chuyện về Đức Khổng Tử, tuy không dài nhưng làm mình suy nghĩ hồi lâu

Từ khóa » Tìm Số Nguyên Tố Gần N