File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes finding factors of a number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "finding factors of a number" Watch "finding factors of a number" New topic
Author

finding factors of a number

Deepika Shet
Greenhorn

Joined: Jan 19, 2013
Posts: 2
solved

Original Question ...


I need to write a program in Java that takes a positive integer and prints out all ways to multiply smaller integers that equal the original number, without repeating sets of factors. Below is my attempt so far at it. Could anyone provide some pointers?


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41791
    
  62
Pointers about what? Correctness? Performance?

First thing I notice is that you're calculating "n / start" a lot. For performance reasons you should retain that value instead of recalculating multiple times. You should also try to avoid division, which tends to be a slow operation compared to multiplication. For correctness reasons you should be aware that dividing integers produces an integer result - possibly not what you want.


Ping & DNS - my free Android networking tools app
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14139
    
  18

There are more efficient ways to check if a number is prime. One easy improvement you can make is that the loop (line 59) only needs to go up to the square root of n, not n itself.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

there are two ways really.

calculate it on the fly, or use the sieve of aristophanes(or however that is spelled).sieve

it depends on the situation of the problem. if you need to go there many times use the sieve if not just do it on the fly


SCJP
Visit my download page
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2280
    
  49
Deepika Shet: Please do not remove your question after it has been answered, it makes the thread useless to others.

For the record the original question was:

I need to write a program in Java that takes a positive integer and prints out all ways to multiply smaller integers that equal the original number, without repeating sets of factors. Below is my attempt so far at it. Could anyone provide some pointers?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18821
    
  40

Tony Docherty wrote:Deepika Shet: Please do not remove your question after it has been answered, it makes the thread useless to others.


I copied the original question back to the first post -- as it is easier to read.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Randall Twede wrote:aristophanes(or however that is spelled)[/url]


Aristophanes was an author (he wrote comedies) in ancient Greece.

Eratosthenes (with the prime sieve) was a Greek mathematician, geographer, poet, astronomer, and music theorist.
He was chief librarian of the Great Library of Alexandria, he measured (calculated) the Earth's circumference,
he founded the science of geography. He must have been a remarkable guy!

The difference between them is more than purely spelling...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finding factors of a number