Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re:need help

  • To: mathgroup at smc.vnet.net
  • Subject: [mg87429] Re: [mg87352] Re:[mg87308] need help
  • From: Patrick Klitzke <philologos14 at gmx.de>
  • Date: Thu, 10 Apr 2008 02:14:04 -0400 (EDT)
  • References: <200804080938.FAA12123@smc.vnet.net> <D1F99877-4BD1-4A2E-AEE2-AEAB4CA94B72@mimuw.edu.pl>

Andrzej Kozlowski wrote:
> On 8 Apr 2008, at 18:38, Patrick Klitzke wrote:
>> But the problem is, that for some numbers you need more than 4 squares:
>> 96=9^2+3^2+2^2+1^2+1^2
>
> A very famous theorem of Lagrange says that every positive integer is 
> a sum of at most 4 squares. This result can be found in virtually 
> every book on number theory (e.g. see K. Chandrasekharan, 
> "Introduction to Analytic number theory"). In the case of 96 we have
>
> PowersRepresentations[96, 4, 2]
> {{0, 4, 4, 8}}
>
> in other words
>
> 96 = 0^2+4^2+4^2+8^2
>
> so in fact you only need 3 squares. Of course if you allow more 
> squares you will get more representations:
>
> PowersRepresentations[96, 5, 2]
> {{0, 0, 4, 4, 8}, {1, 1, 2, 3, 9}, {1, 1, 3, 6, 7},
>    {1, 3, 5, 5, 6}, {2, 2, 4, 6, 6}, {2, 3, 3, 5, 7}}
>
> Also, the question of finding just one representation without 
> restriction on length is trivial since you can represent any number as 
> a sum of squares of 1's !
>
> Andrzej Kozlowski
>
>
>
>
You are right, I used a very bad algorithm and so I could not find a 
solution for even 96.
But I think he want to use the function PowersRepresentations like that:
Sumf[x_] := (
  data = PowersRepresentations[x, 4, 2][[1]];
  If[data[[1]] == 1, data[[1]] = "1"];
  If[data[[2]] == 1, data[[2]] = "1"];
  If[data[[3]] == 1, data[[3]] = "1"];
  If[data[[4]] == 1, data[[4]] = "1"];
  Row[{data[[1]]^"2", data[[2]]^"2", data[[3]]^"2", data[[4]]^"2"},
   "+"]
  )

And here is my "better" function:
---------------------------------------------------
Sumf[x_] :=
 (
  s = x; m = x; s1 = x;
  s = m;
 
  While[
   IntegerQ[Sqrt[s1]] == False, s1--
   ];
  s -= s1; s2 = s;
  While[
   IntegerQ[Sqrt[s2]] == False, s2--
   ];
  s -= s2; s3 = s;
  While[
   IntegerQ[Sqrt[s3]] == False, s3--
   ];
  s -= s3; s4 = s;
  While[
   IntegerQ[Sqrt[s4]] == False, s4--
   ];
  s -= s4;
 
 
  While[
    s != 0,
    {
     s2--;
     If[s2 == 0, {s1--, s2 = m - s1}];
     s = m;
    
     While[
      IntegerQ[Sqrt[s1]] == False, s1--];
     s -= s1;
     While[
      IntegerQ[Sqrt[s2]] == False, s2--
      ];
     s -= s2; s3 = s;
     While[
      IntegerQ[Sqrt[s3]] == False, s3--
      ];
     s -= s3; s4 = s;
     While[
      IntegerQ[Sqrt[s4]] == False, s4--
      ]; s -= s4;
     }
    ]
    
   If[s1 == 1, s1 = "1", s1 = Sqrt[s1]];
  If[s2 == 1, s2 = "1", s2 = Sqrt[s2]];
  If[s3 == 1, s3 = "1", s3 = Sqrt[s3]];
  If[s4 == 1, s4 = "1", s4 = Sqrt[s4]];
 
  Print[Row[{s1^"2", s2^"2", s3^"2", s4^"2"}, "+"], "  Remainder:", s,
    " number: ", x]
 
  )
-----------------------------------------------
I have tested it from 0 -10000 and only the number 7168 could not be 
expressed.

I hope this could help.

Best regards

Patrick Klitzke



  • References:
  • Prev by Date: Re: Pattern problem: How to count from a long list of numbers all
  • Next by Date: Re: Bug in ExportString?
  • Previous by thread: Re: Re:need help
  • Next by thread: Re: need help