Math Is Fun Forum

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

You are not logged in.

#26 2014-03-01 18:51:27

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

Re: Four Squares Reborn

Hi,

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

In J, it's simple:

(1+i.100)^2

Range[100]^2 in Mm?


"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

#27 2014-03-01 18:59:37

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

Re: Four Squares Reborn

Hi;


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

#28 2014-03-01 19:05:25

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

Re: Four Squares Reborn

Functional programming is very powerful once we get a hang of it.


"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

#29 2014-03-01 19:09:59

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

Re: Four Squares Reborn

I agree, I am hoping I can convince Agnishom of that.


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

#30 2014-03-01 19:12:16

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

Re: Four Squares Reborn

Anybody will, once they realize how compact the code can get!


"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

#31 2014-03-01 19:18:58

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

Re: Four Squares Reborn

Yes, he is a very bright guy and luckily he does not have lots of years of programming in procedural languages to make it difficult.


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

#32 2014-03-01 19:21:45

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

Re: Four Squares Reborn

That's right.


"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

#33 2014-03-01 19:33:30

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

Re: Four Squares Reborn

Did you see the new problem in Bafflers?


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

#34 2014-03-02 00:16:05

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

Re: Four Squares Reborn

Yes, I was trying a way to use only the formulas for sector and square, could not proceed that way.


"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

#35 2014-03-02 00:19:46

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

Re: Four Squares Reborn

Oh okay, you will get it.


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

#36 2014-03-02 01:08:13

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

Re: Four Squares Reborn

bobbym wrote:

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

Python Code:

for i in xrange(1,101): print i**2

Functional Way (Python):

map(lambda x: x**2,xrange(1,101))

Maxima Code:

for a: 1 thru 100 do display(a^2);

Functional Way (Maxima):

makelist(k^2,k,1,100);

Mathematica Code:

Function[x,x^2] /@ Range[1,100]

or

Map[Function[x,x^2],Range[1,100]]

'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

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

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

Re: Four Squares Reborn

Simpler is Range[100]^2.


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

#38 2014-03-02 01:21:47

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

Re: Four Squares Reborn

But that is a matrix operation of which I know nothing.

he does not have lots of years of programming in procedural languages to make it difficult.

I've got atleast 7 years of Procedural experience.


'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

#39 2014-03-02 01:27:30

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

Re: Four Squares Reborn

Nope, that is not a matrix operation. You are thinking it is like m x m but it is not.

Range[100] generates a list from1 to 100. Range[100]^2 means square every element of the list. This is why loops are unnecessary. In functional languages commands work on entire lists as if they were single objects. This is more readable and saves the programmer the details of indexing an array.


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

#40 2014-03-02 01:30:24

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

Re: Four Squares Reborn

Range[100]^2 means Range[100] multiplied with Range[100]. Which is multiplying the matrix [1,2,3,...100] with the matrix [1,2,3,...100]. That is how it works.


'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

#41 2014-03-02 01:35:11

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

Re: Four Squares Reborn

It is not a matrix, it is a list. Let me illustrate.

What do you think Range[10]^(1/2) 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

#42 2014-03-02 01:36:07

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

Re: Four Squares Reborn

If you are thinking that InsertSomeFunction[Range[100]] is the same as InsertSomeFunction /@ Range[100] then you are wrong.

Proof. Assume the contrary.

See this counterexample: there is a difference between Speak[Range[100]] is not the same as Speak /@ Range[100]

It works in the other case because it is a matrix operation.

What do you think Range[10]^(1/2) does?

It multiplies the matrix {1,2,3,4,5,6,7,8,9,10} with the constant 1/2

Last edited by Agnishom (2014-03-02 01:37:32)


'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

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

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

Re: Four Squares Reborn

It is not a matrix, it is a list. Let me illustrate.

What do you think Range[10]^(1/2) 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

#44 2014-03-02 01:38:41

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

Re: Four Squares Reborn

What do you think Range[10]^(1/2) does?

It multiplies the matrix {1,2,3,4,5,6,7,8,9,10} with the constant 1/2

A matrix and a list is the same thing

Last edited by Agnishom (2014-03-02 01:40:22)


'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

#45 2014-03-02 01:43:33

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

Re: Four Squares Reborn

That is incorrect.

Range[10]^(1/2)

Range[10]^n

See how it was mapped to each element. Single brackets are lists.

Sin[Range[10]]


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

#46 2014-03-02 01:45:42

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

Re: Four Squares Reborn

That is incorrect.

Sorry, I meant that it raises the matrix {1,2,3,4,5,6,7,8,9,10} to the power 0.5

Then why isn't Speak[Range[100]] and Speak /@ Range[100] the same?


'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

#47 2014-03-02 01:49:56

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

Re: Four Squares Reborn

Speak does that because it treats the argument as a string. Just reading what it sees. Because for most functions have the attribute called listable. That means that they map themselves over lists.

{1,2,3,4,5,6,7,8,9,10} is not a matrix, it is a list. In M, matrices are lists inside of lists. {{1,2,3},{4,5,6},{7,8,9}} that is a matrix. You see, in M everything is a list just like lisp.


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

#48 2014-03-02 01:53:53

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

Re: Four Squares Reborn

Is it okay to say that for any list L, f[L] does the same thing as f /@ L?


'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

#49 2014-03-02 01:57:16

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

Re: Four Squares Reborn

Yes, in all cases that I have seen that will be true. The \@ means map. Just like a mapping in mathematics. It maps the function f onto all the elements of the list.

Please do not get confused here. In M, the list is used to represent vectors, matrices, sets, and arrays. It depends on the context and what commands you are using afterward whether you are speaking of a list or a vector.


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

#50 2014-03-02 02:03:58

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

Re: Four Squares Reborn

Okay, can we get back to explaining that code?


'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

Board footer

Powered by FluxBB