-4

I have the following code:

public static void main (String args[])
{

    Scanner input = new Scanner(System.in);
    System.out.println("\nLab1a\n");
    final int MAX = 100;
    boolean primes[];
    primes = new boolean[MAX];


    ArrayList<Integer>PrimeFactor = new ArrayList<Integer>();
    for (int i = 1; i < MAX + 1; i++)
    {
       PrimeFactor.add(i);
    }

    System.out.println("Computing Prime Numbers");
    System.out.println();
    System.out.println("Primes Between 1 and 100");
    System.out.println(PrimeFactor);
}

public static void CompositeNumbers(ArrayList<Integer> PrimeFactor)
{
    for (int i = 0; i < PrimeFactor.size(); i++)
    {
        if (isPrime(PrimeFactor.get(i)))
        { PrimeFactor.remove(i);}
    }
}

public static boolean isPrime(int n)
{
    for (int i = 2; i < n; i++)
    {
        if (n%i == 0)
          return false;
    }
    return true;
}

}

So what I want to do is output the prime numbers from the numbers 1 to 100. The problem is whenever I output, I only get the numbers 1 to 100. How can I fix that? I see the problem because PrimeFactor is being used for two different things, I'm not sure. So please help.

Bob
  • 23
  • 7

3 Answers3

1

In addition to Mr Dev's code, I would like to add.

public static void CompositeNumbers(ArrayList<Integer> PrimeFactor)
{
    for (int i = 0; i < PrimeFactor.size(); i++)
    {
        if (!isPrime(PrimeFactor.get(i)))
        { 
            PrimeFactor.remove(i);
            i--;  //<--

        }
    }
}

The array list will shrink after you remove an item. This decrement will recheck the element at i once the array adjusts.

Blubber
  • 1,123
  • 1
  • 11
  • 28
0

The answer.

public static void main (String args[])
{

    Scanner input = new Scanner(System.in);
    System.out.println("\nLab1a\n");
    final int MAX = 100;
    boolean primes[];
    primes = new boolean[MAX];

    ArrayList<Integer>PrimeFactor = new ArrayList<Integer>();
    for (int i = 2; i < MAX + 1 ; i++)
    {
        PrimeFactor.add(i);
    }

    CompositeNumbers(PrimeFactor);
    System.out.println("COMPUTING RIME NUMBERS");
    System.out.println();
    System.out.println("PRIMES BETWEEN 1 AND 100");
    CompositeNumbers(PrimeFactor);
    System.out.println(PrimeFactor);
}

public static void CompositeNumbers(ArrayList<Integer> PrimeFactor)
{
    for (int i = 0; i < PrimeFactor.size(); i++)
    {
        if (!isPrime(PrimeFactor.get(i)))
        { 
            PrimeFactor.remove(i);
            i--;
        }
    }
}

public static boolean isPrime(int n)
{
    if(n==1)
    {
        return true;
    }
    for (int i = 2; i < n +1/2; i++)
    {
        if (n%i == 0)
        {
            return false;
        }
    }
    return true;
}

}

Bob
  • 23
  • 7
-1

You should call

CompositeNumbers(PrimeFactor);

in your main befor printing PrimeFactor

public static void main (String args[])
{

    Scanner input = new Scanner(System.in);
    System.out.println("\nLab1a\n");
    final int MAX = 100;
    boolean primes[];
    primes = new boolean[MAX];


    ArrayList<Integer>PrimeFactor = new ArrayList<Integer>();
    for (int i = 1; i < MAX + 1; i++)
    {
       PrimeFactor.add(i);
    }

    CompositeNumbers(PrimeFactor);
    System.out.println("Computing Prime Numbers");
    System.out.println();
    System.out.println("Primes Between 1 and 100");
    System.out.println(PrimeFactor);
}

public static void CompositeNumbers(ArrayList<Integer> PrimeFactor)
{
    for (int i = 0; i < PrimeFactor.size(); i++)
    {
        if (!isPrime(PrimeFactor.get(i)))
        { 
            PrimeFactor.remove(i);
        }
    }
}

public static boolean isPrime(int n)
{
    if(n==1)
    {
        return true;
    }
    for (int i = 2; i < n+1/2; i++)
    {
        if (n%i == 0)
        {
            return false;
        }
    }
    return true;
}
Devid Farinelli
  • 7,020
  • 8
  • 35
  • 65
  • You should add CompositeNumbers(PrimeFactor); before System.out.println(PrimeFactor); – Devid Farinelli Mar 29 '15 at 01:05
  • Actually, I have another problem. I put the CompositeNumbers(PrimeFactor); but now my output is [1, 2, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] – Bob Mar 29 '15 at 01:57
  • Okay, It works thank you. – Bob Mar 29 '15 at 03:31