8 Replies - 1521 Views - Last Post: 01 July 2011 - 08:58 PM Rate Topic: -----

#1 zafarj   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 79
  • Joined: 01-July 11

Prime factorisation

Posted 01 July 2011 - 08:13 PM

The question is to find all the factorization of number.
To find the factors, repeat the following until number is equal to 1:
▪ Start with a factor of 2 each time through the loop
▪ As long as number is not divisible by the current factor (result from modulus is not 0), increase the
factor by 1
▪ Once you've found a factor (result from modulus is 0), add the factor to your list (add it to the String) and
then divide number by the factor
◦ Return the String that contains all the factors (separated by spaces)

public static String findPrimeFactorization(int number)
  {
    String factorise = " ";
    int i;
    for(i = 2; i < number; i++){
      
      while(number % i == 0)
      {
        factorise = factorise + " " + i);
        number /= i;
      }

     }
 
      return factorise;
  }

I am getting an error:
Syntax error on token ")", delete this token

What should I do?

View Postzafarj, on 01 July 2011 - 08:09 PM, said:

The question is to find all the factorization of number.
To find the factors, repeat the following until number is equal to 1:
▪ Start with a factor of 2 each time through the loop
▪ As long as number is not divisible by the current factor (result from modulus is not 0), increase the
factor by 1
▪ Once you've found a factor (result from modulus is 0), add the factor to your list (add it to the String) and
then divide number by the factor
◦ Return the String that contains all the factors (separated by spaces)

public static String findPrimeFactorization(int number)
  {
    String factorise = " ";
    int i;
    for(i = 2; i < number; i++){
      
      while(number % i == 0)
      {
        factorise = factorise + " " + i;
        number /= i;
      }

     }
 
      return factorise;
  }

I am getting wrong answer:
2 2 5 5


What should I do?


Is This A Good Question/Topic? 0
  • +

Replies To: Prime factorisation

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Prime factorisation

Posted 01 July 2011 - 08:13 PM

factorise = factorise + " " + i); // <---- there is an extra ) here
Was This Post Helpful? 0
  • +
  • -

#3 zafarj   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 79
  • Joined: 01-July 11

Re: Prime factorisation

Posted 01 July 2011 - 08:14 PM

View Postpbl, on 01 July 2011 - 08:13 PM, said:

factorise = factorise + " " + i); // <---- there is an extra ) here

yeah I got that but answers are wrong
Was This Post Helpful? 0
  • +
  • -

#4 zafarj   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 79
  • Joined: 01-July 11

Re: Prime factorisation

Posted 01 July 2011 - 08:21 PM

Dont worry I got the answers
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Prime factorisation

Posted 01 July 2011 - 08:27 PM

You just have to follow step by step your instructor instructions
	public static String findPrimeFactorization(int number)
	{
		String factorise = " ";

		for(int factor  = 2; factor < number; factor++){
			// As long as number is not divisible by the current factor (result from modulus is not 0), increase the factor by 1
			// so we will go to the next iteration of the loop thus doing the +1
			if(number % factor == 0) {
				// Once you've found a factor (result from modulus is 0), add the factor to your list (add it to the String) and then divide number by the factor
				factorise = factorise + " " + factor;
				number /= factor;
			}
		}

		return factorise;
	}


But I guess your instructor instructions really work
you'll have to reset factor which he does not say :)

This post has been edited by pbl: 01 July 2011 - 08:31 PM

Was This Post Helpful? 1
  • +
  • -

#6 zafarj   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 79
  • Joined: 01-July 11

Re: Prime factorisation

Posted 01 July 2011 - 08:33 PM

import javax.swing.*;
import java.util.*;

public class JavedZafarA3Q2{
  public static void main(String []args)
  {
    int inputNumber = getInput();
    String primeNumbers = findPrimeNumbers(inputNumber);
    String primeFactorization = findPrimeFactorization(inputNumber);
    String binary = convertToBinary(inputNumber);
    System.out.println("Prime numbers are: " + primeNumbers);
    System.out.println("Prime factors are: " + primeFactorization);
    System.out.println("binary: " + binary);
  }
public static int getInput()
  {
    Scanner scan;
    boolean valid = false;
    String input;
    int value = 0;
    while(!valid){
      input = JOptionPane.showInputDialog("Enter a positive integer number");
      scan = new Scanner(input);
      // check if the user hit close or cancel
      if (input == null) {
        System.out.println("RESPONSE:\n" + input);
        System.out.println();
        System.out.println("PROMPT:\nInput cancelled.");
        
        // check if the string is empty
        if (input.length() < 0) {
          System.out.println("RESPONSE:\n" + input);
          System.out.println();
          System.out.println("PROMPT:\nInput was not a positive integer.");
          
          // check if the input starts with a number
             
          if (!scan.hasNextInt()) {
            System.out.println("RESPONSE:\n" + input);
            System.out.println();
            System.out.println("PROMPT:\nInput was not a valid number.");
            valid = false;
          }
        }
      }
      else
      {
        value = scan.nextInt();
        valid = true;
      }
    }
    System.out.println("RESPONSE:\n" + value);
    return value;
  }


I am getting errors:
File: C:\Users\Zafar Javed\Desktop\University of Manitoba\Computer Science 1010\Assignments\3\JavedZafarA3Q2.java [line: 31]
Warning: Null pointer access: The variable input can only be null at this location

So how should I check that if number is less than zero then it should give me prompt message telling that the number you entered is not positive and ask me again to input positive integer

This post has been edited by zafarj: 01 July 2011 - 08:37 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Prime factorisation

Posted 01 July 2011 - 08:39 PM

That would make more sense... I guess :)

	public static String findPrimeFactorization(int number)
	{
		String factorise = " ";
		while(number > 0) {
			int factor;
			for(factor  = 2; factor < number; factor++){
				// As long as number is not divisible by the current factor (result from modulus is not 0), increase the factor by 1
				// so we will go to the next iteration of the loop
				if(number % factor == 0) {
					// Once you've found a factor (result from modulus is 0), add the factor to your list (add it to the String) and then divide number by the factor
					factorise = factorise + " " + factor;
					number /= factor;
					break;
				}
			}
			if(factor == number) {
				factorise = factorise + " " + factor;					
				break;
			}
		}
		return factorise;
	}


Was This Post Helpful? 0
  • +
  • -

#8 zafarj   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 79
  • Joined: 01-July 11

Re: Prime factorisation

Posted 01 July 2011 - 08:52 PM

View Postpbl, on 01 July 2011 - 08:39 PM, said:

That would make more sense... I guess :)

	public static String findPrimeFactorization(int number)
	{
		String factorise = " ";
		while(number > 0) {
			int factor;
			for(factor  = 2; factor < number; factor++){
				// As long as number is not divisible by the current factor (result from modulus is not 0), increase the factor by 1
				// so we will go to the next iteration of the loop
				if(number % factor == 0) {
					// Once you've found a factor (result from modulus is 0), add the factor to your list (add it to the String) and then divide number by the factor
					factorise = factorise + " " + factor;
					number /= factor;
					break;
				}
			}
			if(factor == number) {
				factorise = factorise + " " + factor;					
				break;
			}
		}
		return factorise;
	}


I can't use break because I never studied that thing in the class and please see to the question which i asked regarding the input
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Prime factorisation

Posted 01 July 2011 - 08:58 PM

View Postzafarj, on 01 July 2011 - 11:52 PM, said:

I can't use break because I never studied that thing in the class and please see to the question which i asked regarding the input

OK, basta, let's add a condition to the loop

	public static String findPrimeFactorization(int number)
	{
		String factorise = " ";
		boolean done = false;      // I can't use break, what the heck
		while(number > 0 && !done) {
			int factor;
			for(factor  = 2; factor < number; factor++){
				if(number % factor == 0) {
					factorise = factorise + " " + factor;
					number /= factor;
					break;
				}
			}
			if(factor == number) {
				factorise = factorise + " " + factor;	
				done = true;   // we are done that will make me exit the while
			}
		}
		return factorise;
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1