Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8851005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:02:14+00:00 2026-06-14T13:02:14+00:00

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

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:02:16+00:00Added an answer on June 14, 2026 at 1:02 pm

    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)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing this Java program that finds all the prime numbers between a given
I'm trying to make a program that finds the largest prime factor of a
import java.util.*; public class PrimeNum { public static void main(String[] args) { Scanner input
I was finding out highest prime factor which divides num, as shown in program,
I am writing this java program to find all the prime numbers up to
How to solve this using scala Actors: I have a program that finds out
This c++ code prints out the following prime numbers: 3 5 7 11 13
I am developing a program where I find myself doing this like this a
This program builds a dictionary out of a list based on what two index
This program returns: package main import ( flag fmt ) func main() { num_agents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.