Để liệt kê các số nguyên tố nhỏ hơn hoặc bằng một số nguyên dương cho trước trong Java, bạn có thể sử dụng thuật toán sàng Eratosthenes.
Thuật toán sàng Eratosthenes là một thuật toán đơn giản để tìm tất cả các số nguyên tố nhỏ hơn hoặc bằng một số nguyên dương n cho trước. Ý tưởng chính của thuật toán này là loại bỏ tất cả các số nguyên tố không phải là số nguyên tố bằng cách sàng lọc qua danh sách các số nguyên.
Dưới đây là đoạn code Java để liệt kê các số nguyên tố nhỏ hơn hoặc bằng một số nguyên dương cho trước bằng thuật toán sàng Eratosthenes:
public static void printPrimes(int n) {
// tạo mảng các số nguyên từ 2 đến n và đánh dấu tất cả các số là số nguyên tố
boolean[] isPrime = new boolean[n+1];
for (int i = 2; i <= n; i++) {
isPrime[i] = true;
}
// sàng lọc các số nguyên tố
for (int i = 2; i*i <= n; i++) {
if (isPrime[i]) {
for (int j = i*i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
// in ra các số nguyên tố
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
System.out.print(i + " ");
}
}
}
Bên cạnh thuật toán sàng Eratosthenes, còn có nhiều cách khác để liệt kê các số nguyên tố trong Java như sử dụng thuật toán kiểm tra đơn giản hoặc thuật toán Miller-Rabin.
Dưới đây là một ví dụ về cách liệt kê các số nguyên tố bằng thuật toán kiểm tra đơn giản:
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void printPrimes(int n) {
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}
Tuy nhiên, thuật toán kiểm tra đơn giản này không hiệu quả khi n rất lớn, vì vậy, thuật toán sàng Eratosthenes là phương pháp tối ưu hơn để liệt kê các số nguyên tố.
Wow, incredible blog format! How long have you ever been blogging
for? you made running a blog look easy. The whole look of your
website is magnificent, as neatly as the content material!
You can see similar here najlepszy sklep
Hello there! Do you know if they make any plugins to help with SEO?
I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains.
If you know of any please share. Thanks! You can read similar
text here: Sklep internetowy
Howdy! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying to get my
blog to rank for some targeted keywords but I’m not seeing very
good results. If you know of any please share. Cheers!
You can read similar art here: Sklep internetowy
Hello! Do you know if they make any plugins to help with SEO?
I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very
good gains. If you know of any please share.
Thank you! You can read similar article here: Backlinks List
Howdy! Do you know if they make any plugins to assist with
Search Engine Optimization? I’m trying to get my site to rank for some targeted keywords but
I’m not seeing very good gains. If you know of any please share.
Thank you! You can read similar text here:
Scrapebox AA List
Wow, fantastic weblog layout! How lengthy have you ever been running a
blog for? you make blogging look easy. The entire glance of your web site is fantastic, as neatly as the content!
You can see similar here sklep internetowy