[sage-support] Re: [sage-devel] Re: [sage-support] Re: sage and sudoku

2007-02-05 Thread William Stein

On Mon, 05 Feb 2007 08:33:44 -0700, Alec Mihailovs [EMAIL PROTECTED] wrote:

 PS It would be interesting if your original code could be modified for
 producing an animation of the magic square - so that the numbers 1, 2, etc.
 appear in the matrix with some time interval between them. I wonder if some
 of included in SAGE packages have such an ability to display an animated
 output -Alec

Currently you can display output as arbitrary javascript, and javascript has
extensive animation capabilities.Just put html  /html around the output
in the print statement, and everything in the html tag is displayed by the
web browser directly as html (hence one can embed javascript).

William

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: [sage-devel] Re: [sage-support] Re: sage and sudoku

2007-02-04 Thread Justin C. Walker


On Feb 4, 2007, at 08:56 , Timothy Clemans wrote:


 Alec is your code suppose to be able to generate any nth normal  
 magic square?

 sage: print magicsquare_normal(4)
 [ 9 15  1  7]
 [14  4  6 12]
 [ 3  5 11 13]
 [ 8 10 16  2]

I think his code just deals with odd 'n' (witness the terms (1 +/-  
n)/2 in the third line).

Justin

--
They said it couldn't be done but sometimes it
doesn't work out that way.
   - Casey Stengel
--



--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: [sage-devel] Re: [sage-support] Re: sage and sudoku

2007-02-04 Thread Alec Mihailovs

 Oh ok then in the code you should write n = 2*n-1 which means if n is
 2 then 3 will be used.

That's not the right way - the argument of the function should be the size 
of the square. If you want to avoid the case of even sizes, that could be 
done by testing the parity - something like

if n%2==0: print The argument should be odd; return

Alec



--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: [sage-devel] Re: [sage-support] Re: sage and sudoku

2007-02-04 Thread Alec Mihailovs

From: Timothy Clemans [EMAIL PROTECTED]

 My function clearly stated magicsquare_normal_odd by being called that
 so its fine and I would just call yours that too. In the docstring I
 would say computes nth odd normal magic square. This function is for
 a special case of normal magic squares.

AFAICT, there is no such thing as a normal magic square. What do you mean by 
normal?

I used 'Siamese' to specify the particular well-known case. If other 
algorithms are planned for including, a better name may be 
magic_square.Siamese , I think. The docstring may be For an odd n, returns 
$n\times n$ Siamese magic square.

Alec 



--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: [sage-devel] Re: [sage-support] Re: sage and sudoku

2007-02-03 Thread Alec Mihailovs

- Original Message - 
From: Timothy Clemans [EMAIL PROTECTED]

 def magicsquare_normal_odd(n):
r
Generates nth odd normal magic square for n greater than 1 using
 de la Loubere's method.

EXAMPLES:
sage: magicsquare_normal_odd(1)
[8 1 6]
[3 5 7]
[4 9 2]
sage: magicsquare_normal_odd(2)
[17 24  1  8 15]
[23  5  7 14 16]
[ 4  6 13 20 22]
[10 12 19 21  3]
[11 18 25  2  9]

--skip--

That can be done in Python in one line,

def Siamese_magic_square(n):
return [[(i+j+(n+1)/2)%n*n+(i+2*j+1)%n+1 for j in range(n)]
   for i in range(n)]

For example,

Siamese_magic_square(3)
[[8, 1, 6], [3, 5, 7], [4, 9, 2]]

Siamese_magic_square(5)
[[17, 24, 1, 8, 15], [23, 5, 7, 14, 16], [4, 6, 13, 20, 22], [10, 12, 19, 
21, 3], [11, 18, 25, 2, 9]]

Alec Mihailovs
http://mihailovs.com/Alec/





--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: [sage-devel] Re: [sage-support] Re: sage and sudoku

2007-02-03 Thread Timothy Clemans

Wow! Now thats cool. I'm going to time test them. Thanks

On 2/3/07, Alec Mihailovs [EMAIL PROTECTED] wrote:

 - Original Message -
 From: Timothy Clemans [EMAIL PROTECTED]
 
  def magicsquare_normal_odd(n):
 r
 Generates nth odd normal magic square for n greater than 1 using
  de la Loubere's method.
 
 EXAMPLES:
 sage: magicsquare_normal_odd(1)
 [8 1 6]
 [3 5 7]
 [4 9 2]
 sage: magicsquare_normal_odd(2)
 [17 24  1  8 15]
 [23  5  7 14 16]
 [ 4  6 13 20 22]
 [10 12 19 21  3]
 [11 18 25  2  9]

 --skip--

 That can be done in Python in one line,

 def Siamese_magic_square(n):
 return [[(i+j+(n+1)/2)%n*n+(i+2*j+1)%n+1 for j in range(n)]
for i in range(n)]

 For example,

 Siamese_magic_square(3)
 [[8, 1, 6], [3, 5, 7], [4, 9, 2]]

 Siamese_magic_square(5)
 [[17, 24, 1, 8, 15], [23, 5, 7, 14, 16], [4, 6, 13, 20, 22], [10, 12, 19,
 21, 3], [11, 18, 25, 2, 9]]

 Alec Mihailovs
 http://mihailovs.com/Alec/





 


--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: [sage-devel] Re: [sage-support] Re: sage and sudoku

2007-02-03 Thread Alec Mihailovs

From: Timothy Clemans [EMAIL PROTECTED]

 Wow! Now thats cool. I'm going to time test them. Thanks

I had some trouble with copying and pasting your procedure in SAGE (because 
I use it in Windows through cygwin and rxvt with Unix line endings and my 
email has Windows line endings and convert it to Unix by creating a text 
file and then using dos2unix on it seemed to be too much trouble). So I 
modified your procedure to plain Python by changing ^ to ** in 2 places and 
changing the end line in it to return square.

After that I did timing in IDLE using the print_timing decorator from 
http://www.daniweb.com/code/snippet368.html .

It appears that Siamese_magic_square is about 4-5 times faster than 
magicsquare_normal_odd.

Actually, if the time is important, it can be made even faster by changing 
the j range (that reduces the number of operations). In SAGE form that looks 
like

def Siamese_magic_square(n):
return matrix([[j%n*n+(j+j-i)%n+1
for j in range(i+(1-n)/2,i+(n+1)/2)] for i in range(n)])

That makes it about 6-7 times faster than magicsquare_normal_odd (in IDLE, 
without matrix - I didn't test that in SAGE and I don't know how the matrix 
construction works there - in particular, whether adding the size of the 
matrix would make it faster.)

Alec Mihailovs
http://mihailovs.com/Alec/ 



--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: [sage-devel] Re: [sage-support] Re: sage and sudoku

2007-02-02 Thread Timothy Clemans

OK I just figured out the coding on my own for following Loubere's
strictly. I want to get the other 3 cases down then optimize them then
make one magicsquare_normal function.

def magicsquare_normal_odd(n):
r
Generates nth odd normal magic square for n greater than 1 using
de la Loubere's method.

EXAMPLES:
sage: magicsquare_normal_odd(1)
[8 1 6]
[3 5 7]
[4 9 2]
sage: magicsquare_normal_odd(2)
[17 24  1  8 15]
[23  5  7 14 16]
[ 4  6 13 20 22]
[10 12 19 21  3]
[11 18 25  2  9]

AUTHOR: Timothy Clemans

square_length = 2*n+1
square = [0 for i in range(square_length^2)]
def position((row,column)):
def row_id(h):
return (h-1)*square_length
return row_id(row)+column-1
for i in range(1,square_length^2+1):
if i == 1:
current_position = (1,n+1)
last_position = current_position
square[position(current_position)] = i
elif last_position == (1,square_length):
current_position = (2,square_length)
last_position = current_position
square[position(current_position)] = i
elif last_position[0] == 1:
current_position = (square_length,last_position[1]+1)
last_position = current_position
square[position(current_position)] = i
elif last_position[1] == square_length:
current_position = (last_position[0]-1,1)
last_position = current_position
square[position(current_position)] = i
elif square[position((last_position[0]-1,last_position[1]+1))]  0:
current_position = (last_position[0]+1,last_position[1])
last_position = current_position
square[position(current_position)] = i
else:
current_position = (last_position[0]-1,last_position[1]+1)
last_position = current_position
square[position(current_position)] = i
return matrix(square_length,square)

On 1/30/07, Timothy Clemans [EMAIL PROTECTED] wrote:
 I think I'm going to come out with my own distribution of SAGE. One of
 the features will that in the cmd line you could type sage -version
 recreation and have an interactive puzzle machine. You could
 personalize it. I would take say Martin Gardner's puzzle books and
 make an interactive version. I think I would even put a mental math
 thing which would take say 50^2 and give different examples of
 computing it using methods from say Math Magic. I could even create
 levels of support with say the magic squares. (SAGE will X amount of
 work based on which level you are on). BTW this how SAGE could become
 a well-known education app. Take the rubics cube and give examples of
 groups with all kinds of interaction. Not even the 3000 system that
 the middle school I went to uses, does this kind of stuff.

 On 1/30/07, David Joyner [EMAIL PROTECTED] wrote:
 
  I think that would be really cool.
 
  Timothy Clemans wrote:
   BTW I mean find magic squares not solve them.
  
   On 1/30/07, Timothy Clemans [EMAIL PROTECTED] wrote:
  
   There is a good book on magic squares. I could start working on
   writing a sage app that solves magic squares.
  
  
   On 1/30/07, Timothy Clemans [EMAIL PROTECTED] wrote:
  
   William Stein wrote it on an airplane.
  
   On 1/30/07, David Joyner [EMAIL PROTECTED] wrote:
  
   Hello:
  
   It's really cool that SAGE has a sudoku solver
   http://modular.math.washington.edu/sage/doc/html/ref/module-sage.games.sudoku.html
   However, the docstring does not have any attributes. No GPL
   blurb, no author, nothing but a short description:
  
   
   Sudoku Solver
  
   Given a 9x9 Sudoku puzzle as an integer matrix, the program solves it.
   
  
   Does anyone know who wrote this?
  
   - David Joyner
  
  
  
   
  
  
 
 
   
 


--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---