Abubakar Abdulkadir
Abubakar Abdulkadir

Reputation: 11

Why is my program to find prime numbers within a range not working the way it's supposed to

I tried creating a program to find prime numbers within a range. The user is asked to input two numbers then the goal of the program will be to print out the prime numbers between the first number and the last number.

Here's my code below:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // program to check prime numbers b/w two numbers
        Scanner sc = new Scanner(System.in);
        byte factorsCount = 0;

        System.out.println("Enter first number: ");
        int numOne = sc.nextInt();
        System.out.println("Enter second number: ");
        int numTwo = sc.nextInt();

        System.out.println("The prime numbers are: ");

        for (int i = numOne+1; i < numTwo; i++) {
            for (int j = 1; j <= i; j++) {
                if (i%j == 0) {
                    factorsCount++;
                }
            }
            if (factorsCount == 2) {
                System.out.println(i);
            }
        }
    }
}

The problem is that the program is not working as intended. For some numOne and numTwo it prints some prime numbers whilst for others it prints nothing

The way I intended for it to work is as follows:

  1. It asks the user for the first number and the second number. It then creates a variable to store the no. of factors a specific number has
  2. The outer for loop iterates through the numbers between the first and second number
  3. The inner for loop counts the number of factors a number has
  4. After the inner loop is done, it breaks out of the inner loop then checks if the factorsCount is equal to 2. if yes, then it is a prime number hence it prints it out and if no it just goes to the next number within the range
  5. It repeats steps one through four until it has exhausted the numbers within the Range after which it must have printed all the prime numbers

Upvotes: 0

Views: 99

Answers (2)

Stefano Riffaldi
Stefano Riffaldi

Reputation: 618

@Paul Stoner write you your code that's work.

I suggest an optimization.

  1. Don't need to check all numbers in range, just odd, so start the outer loop with the first odd after numOne and increment by 2, implicit excluding all evens
  2. Don't need to check 1 and itself as divisor, we know that all number are always divisible by 1 and itself, so exclude it from inner loop.
  3. Don't need to check all divisor, you need to find first odd (because if a number can be divided by an even is even itself and it's excluded on top). At the first occurrence, you number are not prime, so you can exit.

in this way

private static void findPrimes(int start, int end) {
    for (int number = start % 2 == 0 ? start + 1 : start + 2; number <= end; number += 2) {
        boolean hasDivisor = false;
        for (int divisor = 3; divisor < number && !hasDivisor; divisor+=2) {
            hasDivisor = number % divisor == 0;
        }
        if (!hasDivisor) {
            System.out.println(number);
        }
    }
}

Upvotes: 0

Paul Stoner
Paul Stoner

Reputation: 1512

Here is a working copy of your code

package com.goodyear;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // program to check prime numbers b/w two numbers
        Scanner sc = new Scanner(System.in);
        byte factorsCount = 0;

        System.out.println("Enter first number: ");
        int numOne = sc.nextInt();
        System.out.println("Enter second number: ");
        int numTwo = sc.nextInt();

        System.out.println("The prime numbers are: ");

        for (int i = numOne+1; i < numTwo; i++) {
            for (int j = 1; j <= i; j++) {
                if (i%j == 0) {
                    factorsCount++;
                }
            }
            if (factorsCount == 2) {
                System.out.println(i);
            }
            factorsCount = 0;
        }
    }
}

Resetting the factorCount variable after evaluation allows for the next evaluation to occur as designed. Also, consider making you evaluation inclusive on numOne and numTwo by changing your loop variables.

Upvotes: 0

Related Questions