Imperative code example.

This commit is contained in:
Paweł Dyda 2022-11-12 20:24:50 +01:00
parent b1e3a5b448
commit 04a7696d53

View File

@ -0,0 +1,32 @@
package pl.amu.edu.demo.primes;
import lombok.NoArgsConstructor;
@NoArgsConstructor
public class UglyImperativePrimes {
public static void main(String[] args) {
new UglyImperativePrimes().printPrimes();
}
public void printPrimes() {
for (int i = 0; i < 121; i++) {
if (isPrime(i)) {
System.out.printf("%d is prime\n", i);
}
}
}
private boolean isPrime(int number) {
if (number <= 0) {
return false;
}
for (int i = 2; i <= (int) Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}