How to find the largest square in the number (Java)
Posted
by
Ypsilon IV
on Stack Overflow
See other posts from Stack Overflow
or by Ypsilon IV
Published on 2011-02-06T15:23:52Z
Indexed on
2011/02/06
15:25 UTC
Read the original article
Hit count: 207
Hello everyone,
I want to find the largest square in the number, but I am stuck at some point. I would really appreciate some suggestions. This is what I've done so far:
I take the number on the input, factorize into prime numbers, and put the sequence of prime numbers to ArrayList. Numbers are sorted, in a sense, thus the numbers in the sequence are increasing. For example,
996 is 2 2 3 83
1000 is 2 2 2 5 5 5
100000 is 2 2 2 2 2 5 5 5 5 5
My idea now is to count number of occurrences of each elements in the sequence, so if the number of occurrences is divisible by two, then this is the square. In this way, I can get another sequence, where the right most element divisible by two is the largest square.
What is the most efficient way to count occurrences in the ArrayList? Or is there any better way to find the largest square?
Many thanks in advance!
© Stack Overflow or respective owner