-2

I am trying to write a program that give an output as prime decomposition of a given number. However, my code gives correct answer as an output of "2**2**2**2**2**5**7**7**11*" but I want it specific output as "(p1**n1)(p2**n2)...(pk**nk)". Here's my code:

public class PrimeDecomp
{
    public static String factors(int n)
    {
        String ans = "";
        for (int i = 2; i <= n; i++)
        {
            if (n % i == 0)
            {
                // checks if i is a divisor of num
                ans += i + "**";
                // writes i in prime factorization
                n = n/i;
                // since it is written down, num=num/i
                i--;
                // just in case their are multiple factors of same number.
                // For example, 12=2*2*3
            }
        }
        return (ans.substring(0, ans.length() - 1));
    }

    public static void main(String[] args)
    {
        System.out.println(PrimeDecomp.factors(86240));
    }
}
Pinkie Swirl
  • 2,375
  • 1
  • 20
  • 25
Ron Ohare
  • 3
  • 6
  • /*public class PrimeDecomp { public static String factors(int n){ String ans=""; for(int i=2;i<=n;i++){ if(n %i==0){ //checks if i is a divisor of num ans+= i+ "**"; //writes i in prime factorization n = n/i; //since it is written down, num=num/i i--; //just in case their are multiple factors of same number. For example, 12=2*2*3 } } return(ans.substring(0,ans.length()-1)); } public static void main(String[] args) { System.out.println(PrimeDecomp.factors(86240)); } }*/ – Ron Ohare Oct 16 '15 at 16:43
  • 3
    You might have noticed that comments don't format code very well. You need to edit your question and add your code there. – azurefrog Oct 16 '15 at 16:43
  • 2
    Make sure you [format it properly](http://stackoverflow.com/editing-help) too, please. – tnw Oct 16 '15 at 16:45
  • 4
    @RonOhare Click [edit](http://stackoverflow.com/posts/33175544/edit) on my comment or just below the tags on your question. Then view the link from my earlier comment on how to format your code. – tnw Oct 16 '15 at 16:46
  • How come you couldn't edit your question? There's the "edit" link at the bottom of your question (below the tags). – Sufian Oct 17 '15 at 06:04

1 Answers1

0

You almost got it, instead of computing one factor at a time, compute all for the same factor and count them:

public static String factors(int n)
{
    String ans = "";
    int count = 0;
    for (int i = 2; i <= n; i++)
    {
        // Reset the counter
        count = 0;

        /*
         * Instead of only processing on factor, we process them all and
         * count them
         */
        while (n % i == 0)
        {
            count++;
            n = n / i;
        }

        // If we have at least processed one add it to the string
        if (count == 1)
        {
            ans += "(" + i + ")";
        } else if (count > 0)
        {
            ans += "(" + i + "**" + count + ")";
        }
    }
    return ans;
}

Since you are manipulating a string quite often in a loop, you should use StringBuilder

Pinkie Swirl
  • 2,375
  • 1
  • 20
  • 25
  • what if it is times 1 and only want the number not times 1? For example: {(2**5)(5**1)(7**2)(11**1)} and I want it as {(2**5)(5)(7**2)(11)}. – Ron Ohare Oct 16 '15 at 18:33
  • Add an if else, the first checks for count == 1, the other for count > 0. In count == 1 you just add the number to the string like ans += "(" + I + ")"; – Pinkie Swirl Oct 16 '15 at 18:44