Almost Lagrange's Four Squares

Let F ( X ) F(X) be the number of ordered pairs ( a , b , c , d ) N (a , b , c , d) \in \mathbb{N} such that,

a 2 + b 2 + c 2 + d 2 = X {a}^2 + {b}^2 + {c}^2 + {d}^2 = X

You are given that F ( 30 ) = 24 F(30) = 24 and that F ( 1000 ) = 204 F(1000) = 204

Find F ( 30000000 ) F(30000000)

EDIT : Your code should run in 20-40s


The answer is 584064.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Atman Kar
Apr 19, 2018

Let a 2 + b 2 = α {a}^2 + {b}^2 = \alpha

c 2 + d 2 = X α \implies {c}^2 + {d}^2 = X - \alpha

Now, we will create a hash-table d d such that,

d d = {A particular Value of α \alpha : Number of ways it can be represented as sum of two perfect squares}

So for example, d [ α = 50 ] d[\alpha = 50] = 2 2 , as 50 can be represented in two ways as sum of two squares, 50 50 = 1 2 + 7 2 {1}^2 + {7}^2 = 5 2 + 5 2 {5}^2 + {5}^2

Now, all we have to do is generate all possible values of α \alpha and store it in d d .

Now, we iterate through each and every element in d d , and check if X α X - \alpha is there in the table(checking existence of an element in a hash table takes O(1) time)

If X α X - \alpha doesn't exist, then X X cannot be represented as sum of four squares for that particular value of α \alpha . If X α X - \alpha does exist, then number of ways to write X X as sum of four squares becomes,

d [ α ] d [ X α ] d[\alpha] * d[X - \alpha]

Using this logic, and little bit of optimization, your code can run in less than 20s

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...