I wrote this very quickly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let sub list n =
  let rec sub_inner list n =
    if (List.length list = n) then [list]
    else 
      match n with
       | 1 -> List.map ( fun u  ->  [ u ])  list
       | _ -> 
          let h,t = List.hd list, List.tl list
          List.append (List.map (fun u -> (h::u)) (sub_inner t (n - 1))) (sub_inner t n)
  if (List.length list < n) then failwith "length must be greater or equal to n"
  else if (n <=0) then failwith "n must be >= 1 "
       else sub_inner list n
   
let max = 16
let order = 4  
let sum = order * (max - 1) / 2 
let magic2 sum list = (List.sum list = sum)   
  
let l = sub [0..max - 1] order |> List.filter (magic2 sum)
  
l |> List.iter (printfn "%A")
By on 6/24/2009 2:08 PM ()

Though written quickly perhaps, it works! thanks, nice code and order and dimension independend (changed max to 25, and order to 5, and obtained the list of order 5 magic lines; changing 16 to 64 my familiar order 4 cube magic lines appears)

By on 6/24/2009 10:05 PM ()

Continuing the fun I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
let square list ord =
   let rec equ lst vl = 
      if (List.length lst = 0) then false 
      elif List.hd lst = vl then true 
      else equ (List.tl lst) vl
   let rec eq lst1 lst2 = 
      match lst1 with
      | [] -> false
      | [ a ] -> not (equ lst2 a)
      |_ -> not (equ lst2 (List.hd lst1)) && eq (List.tl lst1) lst2
   let rec lines list ord =
      if (List.length list = 0) then []
      elif (List.length list = ord) then [list]
      else
         let h,t = List.hd list, List.tl list
         let tf = List.filter (fun u -> eq h u) list
         //if (ord = 2) then
         // List.map (fun u -> (h::u)) tf
         //else
         List.append (List.map (fun u -> (h::u)) (lines tf (ord - 1))) (lines t ord)
   lines list ord |> List.filter (fun u -> eq u.[ord-2] u.[ord-1])
 
let squares = square l order
squares.Length |> printfn "%d"
squares |> printfn "%A"

which works, attempts to work the last filter into 'lines' is the ord = 2 statement, viriations on the statement above tried but everything ran up to a 'ranking problem' also I reckon the equ/eq combination can be stated using List.find, used here to list in tf the sublist holding the lines of numbers with number not in h thus forming squares of unique numbers.

As currently is you find the last set of numbers with possibly equal numbers as the previous set, this is filtered out by the last filter.
The ord = 2 statement has I reckon (debugging in VS2008 ctp, isn't possible) a head with all tails already ok possibilities, but yet to find a stement combining h with all tails.

Squares nicely hold the expected square, manupulation of which can be semi-magic squares by permutation of the lines above, permuting the lines one can get to the 880 possible magic squares of order 4. (just a goal of this little excercise)
The cube variation seems to be too lengthy (in duration) which prompts the question whether it is possible to halt a given iteration at some point, and continue at a later date?

By on 6/26/2009 3:46 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper