Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2014-02-28 02:25:10

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Four Squares Reborn

Problem
You need to find (at most) 20 ways  of representing a natural number as a sum of (at most) four perfect squares.

Details
The number can be upto 10 digits large.

Last edited by Agnishom (2014-02-28 02:26:03)


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#2 2014-02-28 02:32:27

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

Hi;

You want 20 instances of a 10 digit number represented as 4 squares? For instance:

22077^2+20975^2+15106^2+8890^2=1234567890

you want 19 more like that?

A first effort with M:

square4[n_] := 
 Block[{ans}, 
  ans = FindInstance[a^2 + b^2 + c^2 + d^2 == n, {a, b, c, d}, 
      Integers, RandomSeed -> #]& /@ Range[20];
  ans = Union[Flatten[ans, 1]];
  If[Length[ans] == 20, {a, b, c, d} /. ans]]

Run square[7777777777]


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#3 2014-02-28 04:11:30

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Four Squares Reborn

In[1]:= square4[n_] := 
 Block[{ans},
  ans = FindInstance[a^2 + b^2  + c^2  + d^2 == n, {a, b, c, d},
      Integers, RandomSeed -> #] & /@ Range[20];
  ans = Union[Flatten[ans, 1]];
  If [Length[ans] == 20, {a, b, c, d} /. ans]]

In[3]:= square4[7777777777]

Out[3]= {{19490, 10320, 82626, 21549}, {31912, 9596, 75889, 
  30136}, {34436, 11990, 57866, 55675}, {38076, 31252, 72801, 
  7164}, {41376, 32166, 69642, 13459}, {45004, 554, 55403, 
  51794}, {45904, 9556, 74332, 7351}, {49972, 41088, 58680, 
  12207}, {51066, 27024, 50026, 44013}, {51074, 17822, 67096, 
  18701}, {55912, 198, 54798, 40605}, {56524, 50588, 43496, 
  11479}, {58572, 30254, 58581, 246}, {60124, 31236, 45207, 
  33816}, {61456, 3434, 58477, 23866}, {61554, 21900, 54806, 
  22485}, {64216, 44750, 29539, 27910}, {66136, 12574, 49501, 
  28202}, {70574, 3300, 46635, 24726}, {84902, 18774, 14724, 411}}

Works!

A Windoze 8 laptop is downloading tons of data whenever I connect it. How do I immediately stop this?


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#4 2014-02-28 04:18:03

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

Very good!. Do you see why it is only a first effort?

Also, please wrap a Timing command around fubar.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#5 2014-02-28 04:39:45

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Four Squares Reborn

What is the secret behind Mathematica's FindInstance command?


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#6 2014-02-28 04:44:00

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

It is the most powerful command that can be had. It uses all known algorithms to answer and solve many, many questions. But you have not answered the question.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#7 2014-02-28 05:01:36

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Four Squares Reborn

In[8]:= ClearSystemCache[]; Timing[fubar[7777777777];]

Out[8]= {0.499203, Null}

How do I program this without a mathematical software?


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#8 2014-02-28 05:05:31

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

A greedy algorithm is probably okay.

But you did see that this is only a first effort. Do you see the one procedural construct in it at the end?


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#9 2014-02-28 05:16:30

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Four Squares Reborn

No??


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#10 2014-02-28 05:20:30

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

If [Length[ans] == 20, {a, b, c, d} /. ans]

An if then statement is a procedural statement. This one says if Length of the answer so far is 20 then print it ( among other things ). fubar assumes that it will always return 20 answers but that is unknown. There may be inputs that do not return 20 answers!


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#11 2014-02-28 05:35:46

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Four Squares Reborn

I do not quite see what is wrong with it.

What is this:  RandomSeed -> #]& /@ Range[20];


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#12 2014-02-28 05:41:36

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

That is tough to understand at first. It just gets 20 random seeds for FindInstance.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#13 2014-02-28 18:38:50

gAr
Member
Registered: 2011-01-09
Posts: 3,482

Re: Four Squares Reborn

In[8]:= ClearSystemCache[]; Timing[fubar[7777777777];]

Out[8]= {0.499203, Null}

And it took 4.37s in raspbian VM, not bad I think.


"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense"  - Buddha?

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

Offline

#14 2014-03-01 02:37:32

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Four Squares Reborn

bobbym wrote:

That is tough to understand at first.

Please explain anyway


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#15 2014-03-01 02:39:12

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

Do you know what the Range command does?


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#16 2014-03-01 02:42:08

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Four Squares Reborn

no


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#17 2014-03-01 02:43:54

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

Okay, did you notice there are no loops in my function?

The reason is functional languages have no need for loops. Commands work on and create whole lists at a time.

Range[10] would produce {1,2,3,4,5,6,7,8,9,10} search the help to see all the options.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#18 2014-03-01 04:07:13

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Four Squares Reborn

But internally, they are programmed through loops. Aren't they?


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#19 2014-03-01 04:10:45

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

I do not know and probably that might be true. But as a high level language the user is taken away from that mode of thinking and brought into a different paradigm. Mind you, the less C++, Basic, Fortran, Pascal, etc you know the better off you are when trying to get into a functional world. That is a personal opinion but I have seen it to be true.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#20 2014-03-01 04:13:58

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Four Squares Reborn

Why should it be a personal opinion? M is optimised for functional programming and I think you are correct and I should think in a Functional approach when programming in M.


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#21 2014-03-01 04:15:38

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

My opinion was this, the more you know of those languages the harder it will be to switch.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#22 2014-03-01 05:01:22

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Four Squares Reborn

Okay, that is a personal opinion. I will not comment on it.


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#23 2014-03-01 05:02:15

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

It can not be helped either. Did you get the Range command?


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#24 2014-03-01 17:32:16

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Four Squares Reborn

Hmm, yes


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#25 2014-03-01 17:58:24

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Four Squares Reborn

How do you get the first 100 squares with your favorite language?


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

Board footer

Powered by FluxBB