Imperative code example.
This commit is contained in:
parent
b1e3a5b448
commit
04a7696d53
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user