Created
May 25, 2021 14:42
-
-
Save bijay-shrestha/d76a037915a23ce57459ae770989f33d to your computer and use it in GitHub Desktop.
Remember: Prime numbers have only two factors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hawa.practice; | |
import lombok.extern.slf4j.Slf4j; | |
import java.util.ArrayList; | |
@Slf4j | |
public class PrimeCount { | |
public static void main(String[] args) { | |
int start = 1, end = 100; | |
log.info("Prime number count between {} and {} is, {}", start, end, primeCount(start, end)); | |
} | |
public static int primeCount(int start, int end) { | |
int count; | |
int primeCount = 0; | |
ArrayList<Integer> primeList = new ArrayList<>(); | |
for (int i = start; i <= end; i++) { | |
count = 0; | |
for (int j = 1; j <= i; j++) { | |
if (i % j == 0) { | |
count++; | |
} | |
} | |
if (count == 2) { | |
primeCount++; | |
primeList.add(i); | |
} | |
} | |
primeList.forEach(p -> log.info("{}, ", p)); | |
return primeCount; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment