Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 574587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:50:49+00:00 2026-05-13T13:50:49+00:00

Given a hypoteneuse ( c in the typical equation a*a + b*b = c*c

  • 0

Given a hypoteneuse (c in the typical equation a*a + b*b = c*c), what is an efficient way to calculate all possible integer values of a and b, such that a < b?

Note: I’ve seen c be greater than 1e12, thus c*c is greater than long.MaxValue, from what I can tell, c*c does fit into a decimal, though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:50:50+00:00Added an answer on May 13, 2026 at 1:50 pm

    There is a mathematical solution that finds a and b fast even for large values of c.
    Unfortunately, it is not that simple. I’m trying to give a short explanation of the algorithm. I hope it is not too confusing.

    Since c is given and you are looking for a and b, you basically want to solve diophantine
    equations of the form

    n=x^2+y^2,
    

    where n is given. It doesn’t help much that n = c*c is a square and thus I’m describing a solution for any n. If n were a prime number then you could use
    Fermat’s theorem, to decide if your equation is solvable, and use, as Moron pointed out the Hermite-Serret algoritm to find the solutions if there are any.

    To solve the case where n is not prime it is a good idea to use
    Gaussian integers. (Gaussian integers are just complex numbers with integral coefficients). In particular one notes that the norm of x+yi is

    N(x+yi) = x^2+y^2.
    

    Hence one has to find the Gaussian integers x+yi whose norm is n.
    Since the norm is is multiplicative it is sufficient to solve this equation for the factors of n and then to multiply the Gaussian integers of the individal equations.
    Let me give an example. We want to solve

    65 = x^2 + y^2.
    

    This is equivalent to find x,y such that

    N(x+yi) = 65
    

    over the Gaussian integers. We factor 65 = 5 * 13, then we use Fermat to note that both
    5 and 13 can be represented as sum of two squares. Finally, we find either by using brute force of by using the Hermite-Serret algorithm

    N(2+i) = N(1+2i) = ... = 5
    N(2+3i) = N(3+2i) = ... = 13
    

    Note, I’ve left out the Gaussion integers 2-i, -2+i, etc with negative coefficients.
    Those are of course solutions too.

    Hence we can now multiply these solutions together to get

    65 = 5*13 = N(2+i) * N(2+3i) = N((2+i) * (2+3i)) = N(1 + 8i)

    and

    65 = 5 * 13 = N(2+i) * N(3+2i) = N((2+i) * (3+2i)) = N(4 + 7i).

    Hence, one gets the two solutions

    1*1 + 8*8 = 65
    4*4 + 7*7 = 65
    

    The other combinations e.g. with negative coefficients need to be checked too.
    They don’t give new solutions other than permutations and changed signs.


    To compute all the solutions one might also add that there is no need to ever compute c*c.
    Finding the factors of c is all that is necessary. Also since a and b are both smaller than c, it will not happen that products of Gaussian integers are not representable with 64-bit integer coefficients. Hence, if one is careful, 64-bit integer are enough precision to solve the problem. Of course, it is always easier to just use a language like Python that does not have this kind of overflow problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 489k
  • Answers 489k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can download the source from here: Cassini for Framework… May 16, 2026 at 8:59 am
  • Editorial Team
    Editorial Team added an answer Best thing to do would be get an account on… May 16, 2026 at 8:59 am
  • Editorial Team
    Editorial Team added an answer Two things: 1) The reason your event is not firing… May 16, 2026 at 8:59 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Given a Python object of any kind, is there an easy way to get
Given a DateTime representing a person's birthday, how do I calculate their age in
I've got a right triangle and I want to check if a given point
In javascript, I am trying to draw a line that is at an angle
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given a select with multiple option's in jQuery. $select = $(<select></select>); $select.append(<option>Jason</option>) //Key =
Given an absolute or relative path (in a Unix-like system), I would like to
Given 2 rgb colors and a rectangular area, I'd like to generate a basic
Okay, so I need to make C go the shortest path from A to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.