This program is supposed to output the prime numbers between 1 and 100. Will anyone please explain me the flow the programme below? I am having difficulty in writing the progra
If you split the various parts out into their own methods with appropriate names it becomes a bit easier to understand:
for (int n = 1; n < 100; n++)
if (isPrime(n))
System.out.println(n);
private boolean isPrime(int n) {
for (int f = 2; f < n; f++) {
if (isFactor(f, n))
return false;
}
return true;
}
private boolean isFactor(int factor, int number) {
return number % factor == 0;
}
This is also an area where Java 8 streams can make things a bit clearer:
List<Integer> primes = IntStream.range(1, 100)
.filter(this::hasNoFactors)
.collect(Collectors.toList());
private boolean hasNoFactors(int number) {
return IntStream.range(2, number)
.noneMatch(f -> number % f == 0);
}
Also note that this is a horribly inefficient algorithm. You don't need to check every possible factor from 2 to n, just the primes. You can also take advantage of multi-processor machines:
List<Integer> primes = new ArrayList<>();
IntStream.range(2, 100)
.filter(n -> primes.parallelStream().noneMatch(p -> n % p == 0))
.forEach(primes::add);
public static ArrayList prime(int limit){
ArrayList<Integer> primes = new ArrayList<>();
for(int p = 2; p <= limit; p++) {
int count = 0;
for(int i=2; i < p; i++) {
if (p%i == 0) {
count++;
}
}
if (count == 0) {
primes.add(p);
}
}
return primes;
}
How would you find a prime number with plain vanilla solution? If number is prime. It will not be a multiple of any number other than itself. So assume number is x. This number will not be divisible by any number when starting from 2 till x-1. Why start from 2 and not 1 because every number is divisible by 1. The above code is trying to replicate the same behavior. To find all primes between 1 till 99 (as per the loop):
The number which is only divisible by itself and 1 is known as prime number. Here is the simplest version of the code for finding prime numbers between 1 to 100.
import java.io.*;
import java.util.*;
class solution
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int n = 100;
/*
A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself.
*/
for(int i=2;i<=n;i++) // 1.So we are starting with initialization i = 2
{
int flag = 1;
for(int j=2;j<=i/2;j++) // 2.Try dividing the number by half check whether it divisible
{
if(i%j==0) // 3. If the number is divisible by other number ->Not a prime Number
{
flag = 0;
break;
}
}
if(flag==1) // 4. If the number is not divisible by any other numbers but only by itself and 1 -> prime no
{
System.out.print(i+" ");
}
}
}
}
/*
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
*/
public static List<Integer> getPrimeNumbers(int from , int to) {
List<Integer> list = new ArrayList<>();
for (int i = from;i <= to; i++) {
int count = 0;
for (int num = i; num>=1;num--) {
if(i%num == 0){
count++;
}
}
if(count ==2) {
list.add(i);
}
}
return list;
}
public class primenum {
public static void main(String[] args) {
for(int i=2;i<100;i++){
int count=1;
for(int j=2;j<i;j++){
if(i%j ==0){
count=0;
break;
}
}if(count==1){
System.out.println(i);
}
}
}