Báiapp

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

package TIM_2_SO_NGUYEN_TO;

public class So_nguyen_to extends Thread {


private int n1;
private int n2;
public So_nguyen_to(int n1, int n2) {
this.n1= n1;
this.n2= n2;
}

public void run ()


{ //hàm này sẽ chạy khi class So_nguyen_to gọi phương thức Start()

System.out.println("Số Nguyên Tố là: ");


//duyệt từ 1 đến 100

for (int i=n1 ; i<=n2; i++) {


//nếu kiểm tra từng phần tử từ 1->100 . nếu đúng là số ng tố sẽ
in ra
this.kiem_tra_so_nguyen_to(i);
try {
Thread.sleep (500); //nghĩ 500 mili giay
}catch (InterruptedException e)
{
}
System.out.println(i+ " của luồng" +this.getName()); //là in ra
số ng tố và lấy tên luồng (getname)
}
}

public boolean kiem_tra_so_nguyen_to(int n) {


if (n < 2) {
return false;// khong co so nguyen to nao nho hon 2
}
int t = (int) Math.sqrt(n);// tra ve can bac hai tham so truyen vao
for (int i = 2; i<= t; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}

You might also like