You are not logged in.

- Topics: Active | Unanswered

**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'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline

```
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'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline

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'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline

```
In[8]:= ClearSystemCache[]; Timing[fubar[7777777777];]
Out[8]= {0.499203, Null}
```

How do I program this without a mathematical software?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline

No??

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline

I do not quite see what is wrong with it.

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

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline

**gAr****Member**- Registered: 2011-01-09
- Posts: 3,479

`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

bobbym wrote:

That is tough to understand at first.

Please explain anyway

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline

no

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline

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

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline

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.

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline

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

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline

Hmm, yes

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 96,652

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.**

Offline