From 04a7696d53701465f2305ceea2218171c1652c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Dyda?= Date: Sat, 12 Nov 2022 20:24:50 +0100 Subject: [PATCH] Imperative code example. --- .../edu/demo/primes/UglyImperativePrimes.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 demo/03/src/main/java/pl/amu/edu/demo/primes/UglyImperativePrimes.java diff --git a/demo/03/src/main/java/pl/amu/edu/demo/primes/UglyImperativePrimes.java b/demo/03/src/main/java/pl/amu/edu/demo/primes/UglyImperativePrimes.java new file mode 100644 index 0000000..55e07ce --- /dev/null +++ b/demo/03/src/main/java/pl/amu/edu/demo/primes/UglyImperativePrimes.java @@ -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; + } + +}