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

You are not logged in.

- Topics: Active | Unanswered

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

Do you understand what a pure function is?

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.** **A number by itself is useful, but it is far more useful to know how accurate or certain that number is.**

Offline

Do LISP programmers call it a lambda function? It is an anonymous function

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

Yes, that is exactly correct. It is borrowed from the Lambda Calculus.

M uses a slot # and a & to make one.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.** **A number by itself is useful, but it is far more useful to know how accurate or certain that number is.**

Offline

Lambda Calculus?

I do not understand the # and & part, it looks very creepy. Please illustrate

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

It is used right here:

```
FindInstance[a^2 + b^2 + c^2 + d^2 == n, {a, b, c, d},
Integers, RandomSeed -> #]& /@ Range[20];
```

But there is one more thing before we get to it...

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.** **A number by itself is useful, but it is far more useful to know how accurate or certain that number is.**

Offline

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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

M uses options to enhance the commands. They are put in like this

(Name of the option) -> value, usually at the end of the 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

Yes, I have seen that. There are a lot of options in the commands that do Interactive things

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

```
FindInstance[a^2 + b^2 + c^2 + d^2 == n, {a, b, c, d},
Integers, RandomSeed -> #]& /@ Range[20];
```

FindInstance uses random methods to arrive at answers as well as all known math methods. Because it sometimes has to use random numbers it requires a seed. Do you know what a random seed?

**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 think it is a number which is used to intiatalise a PRNG

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

That is correct so instead of doing

FindInstance[a^2 + b^2 + c^2 + d^2 == n, {a, b, c, d},Integers, RandomSeed -> 1]

FindInstance[a^2 + b^2 + c^2 + d^2 == n, {a, b, c, d},Integers, RandomSeed -> 2]

FindInstance[a^2 + b^2 + c^2 + d^2 == n, {a, b, c, d},Integers, RandomSeed -> 3]

.

.

.

FindInstance[a^2 + b^2 + c^2 + d^2 == n, {a, b, c, d},Integers, RandomSeed -> 20]

to get 20 different seeds ( remember we want 20 different answers and hope that if he starts from different seeds he will get a different answer). We map the slot operator # to {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20} which does the above but without all the work of writing that.

**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 do you have to supply the seed manually?

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

Because the seed could be anything, how can M know which one I want?

**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 this?

ans = Union[Flatten[ans, 1]];

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

Flatten removes all dimensionality and returns a list. Union is the same as the mathematical set 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

What would the output look like without that command?

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

Quite confusing. Generally the Flatten command does this,

Flatten[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}]

{1, 2, 3, 4, 5, 6, 7, 8, 9}

notice all list brackets were destroyed and the matrix is flattened into a list.

**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 did you use it in that code?

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

I wanted to make sure the Union command and the Length command would work correctly.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

Can we do this instead?

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

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

We sure could but is it anymore correct then what I have done? I mean, we are still gambling that 20 different seeds will coax FindInstance to get the 20 different answers we require.

**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 was just asking.

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

It will run if that is what you mean and get the correct answer.

**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, May I ask you a question that you'll not enjoy answering?

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 104,288

Hmmm, I am afraid to ask. What is 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