Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -13,4 +13,4 @@ For example, given 5, produce this array:
12 11 10 9 8
</pre>
;See also [[Zig-zag matrix]]
;See also [[Zig-zag matrix]] and [[Ulam_spiral_(for_primes)]]

View file

@ -0,0 +1,56 @@
$ p1 = f$integer( p1 )
$ max = p1 * p1
$
$ i = 0
$ r = 1
$ rd = 0
$ c = 1
$ cd = 1
$ loop:
$ a'r'_'c' = i
$ nr = r + rd
$ nc = c + cd
$ if nr .eq. 0 .or. nc .eq. 0 .or. nr .gt. p1 .or. nc .gt. p1 .or. f$type( a'nr'_'nc' ) .nes. ""
$ then
$ gosub change_directions
$ endif
$ r = r + rd
$ c = c + cd
$ i = i + 1
$ if i .lt. max then $ goto loop
$ length = f$length( f$string( max - 1 ))
$ r = 1
$ loop2:
$ c = 1
$ output = ""
$ loop3:
$ output = output + f$fao( "!#UL ", length, a'r'_'c' )
$ c = c + 1
$ if c .le. p1 then $ goto loop3
$ write sys$output output
$ r = r + 1
$ if r .le. p1 then $ goto loop2
$ exit
$
$ change_directions:
$ if rd .eq. 0 .and cd .eq. 1
$ then
$ rd = 1
$ cd = 0
$ else
$ if rd .eq. 1 .and. cd .eq. 0
$ then
$ rd = 0
$ cd = -1
$ else
$ if rd .eq. 0 .and. cd .eq. -1
$ then
$ rd = -1
$ cd = 0
$ else
$ rd = 0
$ cd = 1
$ endif
$ endif
$ endif
$ return

View file

@ -0,0 +1,33 @@
defmodule RC do
def spiral_matrix(n) do
right(n,n-1,0,[]) |> Enum.with_index |> Enum.sort |> Enum.with_index |>
Enum.each(fn {{_,x},i} ->
:io.format("~2w ", [x])
if( rem(i+1,n)==0, do: IO.puts "")
end)
end
def right(n,side,i,coordinates) do
coord = for j <- 0..side, do: {i, i+j}
down(n,side,i,coordinates++coord)
end
def down(_,0,_,coordinates), do: coordinates
def down(n,side,i,coordinates) do
coord = for j <- 1..side, do: {i+j, n-1-i}
left(n,side-1,i,coordinates++coord)
end
def left(n,side,i,coordinates) do
coord = for j <- 0..side, do: {n-1-i, i+side-j}
up(n,side,i,coordinates++coord)
end
def up(_,0,_,coordinates), do: coordinates
def up(n,side,i,coordinates) do
coord = for j <- 1..side, do: {i+side-j+1, i}
right(n,side-1,i+1,coordinates++coord)
end
end
RC.spiral_matrix(5)

View file

@ -0,0 +1,67 @@
(function (n) {
// Spiral: the first row plus a smaller spiral rotated 90 degrees clockwise
function spiral(lngRows, lngCols, nStart) {
return lngRows ? [range(nStart, (nStart + lngCols) - 1)].concat(
transpose(
spiral(lngCols, lngRows - 1, nStart + lngCols)
).map(reverse)
) : [
[]
];
}
// rows and columns transposed (for 90 degree rotation)
function transpose(lst) {
return lst.length > 1 ? lst[0].map(function (_, col) {
return lst.map(function (row) {
return row[col];
});
}) : lst;
}
// elements in reverse order (for 90 degree rotation)
function reverse(lst) {
return lst.length > 1 ? lst.reduceRight(function (acc, x) {
return acc.concat(x);
}, []) : lst;
}
// [m..n]
function range(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
});
}
// TESTING
var lstSpiral = spiral(n, n, 0);
// OUTPUT FORMATTING - JSON and wikiTable
function wikiTable(lstRows, blnHeaderRow, strStyle) {
return '{| class="wikitable" ' + (
strStyle ? 'style="' + strStyle + '"' : ''
) + lstRows.map(function (lstRow, iRow) {
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) {
return typeof v === 'undefined' ? ' ' : v;
}).join(' ' + strDelim + strDelim + ' ');
}).join('') + '\n|}';
}
return [
wikiTable(
lstSpiral,
false,
'text-align:center;width:12em;height:12em;table-layout:fixed;'
),
JSON.stringify(lstSpiral)
].join('\n\n');
})(5);

View file

@ -0,0 +1 @@
[[0,1,2,3,4],[15,16,17,18,5],[14,23,24,19,6],[13,22,21,20,7],[12,11,10,9,8]]

View file

@ -0,0 +1,43 @@
immutable Spiral
m::Int
n::Int
cmax::Int
dir::Array{Array{Int,1},1}
bdelta::Array{Array{Int,1},1}
end
function Spiral(m::Int, n::Int)
cmax = m*n
dir = Array{Int,1}[[0,1], [1,0], [0,-1], [-1,0]]
bdelta = Array{Int,1}[[0,0,0,1], [-1,0,0,0],
[0,-1,0,0], [0,0,1,0]]
Spiral(m, n, cmax, dir, bdelta)
end
function spiral(m::Int, n::Int)
0<m&&0<n || error("The matrix dimensions must be positive.")
Spiral(m, n)
end
spiral(n::Int) = spiral(n, n)
type SpState
cnt::Int
dirdex::Int
cell::Array{Int,1}
bounds::Array{Int,1}
end
Base.length(sp::Spiral) = sp.cmax
Base.start(sp::Spiral) = SpState(1, 1, [1,1], [sp.n,sp.m,1,1])
Base.done(sp::Spiral, sps::SpState) = sps.cnt > sp.cmax
function Base.next(sp::Spiral, sps::SpState)
s = sub2ind((sp.m, sp.n), sps.cell[1], sps.cell[2])
if sps.cell[rem1(sps.dirdex+1, 2)] == sps.bounds[sps.dirdex]
sps.bounds += sp.bdelta[sps.dirdex]
sps.dirdex = rem1(sps.dirdex+1, 4)
end
sps.cell += sp.dir[sps.dirdex]
sps.cnt += 1
return (s, sps)
end

View file

@ -0,0 +1,24 @@
using Formatting
function width{T<:Integer}(n::T)
w = ndigits(n)
n < 0 || return w
return w + 1
end
function pretty{T<:Integer}(a::Array{T,2}, indent::Int=4)
lo, hi = extrema(a)
w = max(width(lo), width(hi))
id = " "^indent
fe = FormatExpr(@sprintf(" {:%dd}", w))
s = id
nrow = size(a)[1]
for i in 1:nrow
for j in a[i,:]
s *= format(fe, j)
end
i != nrow || continue
s *= "\n"*id
end
return s
end

View file

@ -0,0 +1,26 @@
n = 5
println("The n = ", n, " spiral matrix:")
a = zeros(Int, (n, n))
for (i, s) in enumerate(spiral(n))
a[s] = i-1
end
println(pretty(a))
m = 3
println()
println("Generalize to a non-square matrix (", m, "x", n, "):")
a = zeros(Int, (m, n))
for (i, s) in enumerate(spiral(m, n))
a[s] = i-1
end
println(pretty(a))
p = primes(10^3)
n = 7
println()
println("An n = ", n, " prime spiral matrix:")
a = zeros(Int, (n, n))
for (i, s) in enumerate(spiral(n))
a[s] = p[i]
end
println(pretty(a))

View file

@ -0,0 +1,10 @@
def spiral_matrix(n)
x, y, dx, dy = -1, 0, 0, -1
fmt = "%#{(n*n-1).to_s.size}d " * n
n.downto(1).flat_map{|x| [x, x-1]}.flat_map{|run|
dx, dy = -dy, dx # turn 90
run.times.map { [y+=dy, x+=dx] }
}.each_with_index.sort.map(&:last).each_slice(n){|row| puts fmt % row}
end
spiral_matrix(5)

View file

@ -0,0 +1,47 @@
Function build_spiral(n)
botcol = 0 : topcol = n - 1
botrow = 0 : toprow = n - 1
'declare a two dimensional array
Dim matrix()
ReDim matrix(topcol,toprow)
dir = 0 : col = 0 : row = 0
'populate the array
For i = 0 To n*n-1
matrix(col,row) = i
Select Case dir
Case 0
If col < topcol Then
col = col + 1
Else
dir = 1 : row = row + 1 : botrow = botrow + 1
End If
Case 1
If row < toprow Then
row = row + 1
Else
dir = 2 : col = col - 1 : topcol = topcol - 1
End If
Case 2
If col > botcol Then
col = col - 1
Else
dir = 3 : row = row - 1 : toprow = toprow - 1
End If
Case 3
If row > botrow Then
row = row - 1
Else
dir = 0 : col = col + 1 : botcol = botcol + 1
End If
End Select
Next
'print the array
For y = 0 To n-1
For x = 0 To n-1
WScript.StdOut.Write matrix(x,y) & vbTab
Next
WScript.StdOut.WriteLine
Next
End Function
build_spiral(CInt(WScript.Arguments(0)))