Bài toán. Nhập số nguyên dương n. Tìm số nguyên tố nhỏ nhất lớn hơn n. Ví dụ:
NGUYENTO.INP | NGUYENTO.OUT |
9 | 11 |
Code tham khảo:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 | import math def nguyenTo(n): if n < 2 : return False else : m = math.sqrt(n) i = 2 while (n % i) and (i < m): i = i + 1 if i > m: return True else : return False fin = open ( "NGUYENTO.INP" , "r" ) fout = open ( "NGUYENTO.OUT" , "w" ) a = fin.readline() n = int (a) x = n + 1 while not (nguyenTo(x)): x + = 1 fout.write( str (x)) fin.close() fout.close() |
Bạn có thể code thử và chấm bài ở đây