|
-
May 21st, 2010, 11:22 AM
#1
Finding Pythagorean Triples
Given a set 'S' of 'N' numbers, find all x, y and z such that x^2 + y^2 = z^2.
One method:
1. I can store the z^2's in a hashmap (put every number squared ^2 into a hashmap). I can do this in time=N with additional space (the hashmap) of N elements.
2. I can then create a nested loop and multiple every number 'i' by every other number 'j' - looking for the result in the hashmap. That'd be N^2 time.
Am I overlooking a "better" way to do this?
And one more question, what particular class or category of algorithms does this problem fall into?
I can leverage HashMap properties to lookup answers but looking for other structures that would help improve this case.
Thanks in advance
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|