Skip to content

Instantly share code, notes, and snippets.

@bijay-shrestha
Created May 25, 2021 14:42
Show Gist options
  • Save bijay-shrestha/d76a037915a23ce57459ae770989f33d to your computer and use it in GitHub Desktop.
Save bijay-shrestha/d76a037915a23ce57459ae770989f33d to your computer and use it in GitHub Desktop.
Remember: Prime numbers have only two factors.
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