1

This program finds prime factors and prints them out like this: factor (order) factor (order)

import java.util.Scanner;
import java.util.ArrayList;

public class Factorise2
{
    public static ArrayList<Integer> get_prime_factors(int number)
    {
//Get the absolute value so that the algorithm works for negative numbers
        int absoluteNumber = Math.abs(number);
        ArrayList<Integer> primefactors = new ArrayList<Integer>();
        ArrayList<Integer> newprimes = new ArrayList<Integer>();
        int b;
        int c = (int)Math.sqrt(absoluteNumber);
//Get the square root so that we can break earlier if it's prime

        for (int j = 2; j <= absoluteNumber;)
        {
//Test for divisibility by j, and add to the list of prime factors if it's divisible. 
            if (absoluteNumber % j == 0)
            {
                primefactors.add(j);
                absoluteNumber /= j;
                c = (int)Math.sqrt(absoluteNumber);
            }
            else
            {
                for (int a = 0; a < newprimes.size();)
                {
//Change j to the next prime
                    b = newprimes.get(a);
                    if (j % b == 0) 
                    {
                        j++;
                        a = 0;
                    }
                    else
                    {
                        a++;
                    }
                }
                newprimes.add(j);
            }
            if (j > c)
            {
                primefactors.add(absoluteNumber);
                break;
            }
        }
        return primefactors;
    }

    public static void main(String[] args)
    {
//Declare and initialise variables
        int number;
        int count = 1;
        Scanner scan = new Scanner(System.in);
//Get a number to work with
        System.out.println("Enter integer to analyse:");
        number = scan.nextInt();
//Get the prime factors of the number
        ArrayList<Integer> primefactors = get_prime_factors(number);
//Group the factors together and display them on the screen
        System.out.print("Prime factors of " + number + " are ");
        primefactors.add(0);
        for (int a = 0; a < primefactors.size() - 1; a++)
        {
            if (primefactors.get(a) == primefactors.get(a+1))
            {
                count++;
            }
            else
            {
                System.out.print(primefactors.get(a) + " (" + count + ") ");
                count = 1;
            }
        }
    }
}

It works for the most part, but when I input 131033809, I get a strange output:

C:\Users\Eamon>java Factorise2
Enter integer to analyse:
4
Prime factors of 4 are 2 (2)
C:\Users\Eamon>java Factorise2
Enter integer to analyse:
128
Prime factors of 128 are 2 (7)
C:\Users\Eamon>java Factorise2
Enter integer to analyse:
9
Prime factors of 9 are 3 (2)
C:\Users\Eamon>java Factorise2
Enter integer to analyse:
121
Prime factors of 121 are 11 (2)
C:\Users\Eamon>java Factorise2
Enter integer to analyse:
131033809
Prime factors of 131033809 are 11447 (1) 11447 (1)

Why doesn't it write

11447 (2)
Roman C
  • 49,761
  • 33
  • 66
  • 176
Eamon Moloney
  • 1,853
  • 4
  • 19
  • 22
  • 5
    You seriously believe that there is even the slightest chance that this is a bug in Java rather than in your understanding of how Java works? Really?? – Hovercraft Full Of Eels Nov 17 '12 at 19:56
  • 1
    Did you even look at the code? There's not much places it could have gone wrong on my end. I flagged you for being unconstructive. – Eamon Moloney Nov 17 '12 at 20:01
  • 1
    Your hubris though needs to be addressed as it will prevent you from solving this. The odds of your finding a true Java bug given your current coding level are astronomically small, so small that you really must look at your question along the lines of "exactly what about Java am I not understanding". You may think of this as being unconstructive, but how you look at a problem matters and will effect how you solve it. You need to do some debugging of your code either with a debugger or with println statements because you *must* accept that the bug is yours until strongly proven otherwise. – Hovercraft Full Of Eels Nov 17 '12 at 20:06
  • 2
    That's why I asked a question. I asked "Is there a bug in Java?", I wasn't saying there was one. Instead of answering my question you went and took the piss out of it. – Eamon Moloney Nov 17 '12 at 20:19

1 Answers1

11

In Java, the Integer datatype is an object. Thus, == operations will only return true if the object identities are the same: note, this does not concern whether the values are actually equal. You should use:

if (primefactors.get(a).equals(primefactors.get(a+1)))
jma127
  • 1,432
  • 9
  • 13
  • 1
    You should mention the cache for `Integer`s of small absolute value - by default `-128` to `127` - as the reason why `==` worked for small prime factors. – Daniel Fischer Nov 17 '12 at 20:05
  • Read the linked answer - basically, Java guarantees that for small values of Integer, etc, == always works; but there's no such guarantee for larger ones. – Sbodd Nov 17 '12 at 20:07