Cut a rectangle: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 11: | Line 11: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F cut_it(=h, =w) |
||
V dirs = [(1, 0), (-1, 0), (0, -1), (0, 1)] |
V dirs = [(1, 0), (-1, 0), (0, -1), (0, 1)] |
||
I h % 2 != 0 |
I h % 2 != 0 |
||
Line 58: | Line 58: | ||
L(h) 1..w |
L(h) 1..w |
||
I (w * h) % 2 == 0 |
I (w * h) % 2 == 0 |
||
print(‘#. x #.: #.’.format(w, h, cut_it(w, h)))</ |
print(‘#. x #.: #.’.format(w, h, cut_it(w, h)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 96: | Line 96: | ||
=={{header|C}}== |
=={{header|C}}== |
||
Exhaustive search on the cutting path. Symmetric configurations are only calculated once, which helps with larger sized grids. |
Exhaustive search on the cutting path. Symmetric configurations are only calculated once, which helps with larger sized grids. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 175: | Line 175: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight>output<syntaxhighlight lang="text">2 x 1: 1 |
||
2 x 2: 2 |
2 x 2: 2 |
||
3 x 2: 3 |
3 x 2: 3 |
||
Line 214: | Line 214: | ||
10 x 8: 11736888 |
10 x 8: 11736888 |
||
10 x 9: 99953769 |
10 x 9: 99953769 |
||
10 x 10: 1124140214</ |
10 x 10: 1124140214</syntaxhighlight> |
||
More awkward solution: after compiling, run <code>./a.out -v [width] [height]</code> for display of cuts. |
More awkward solution: after compiling, run <code>./a.out -v [width] [height]</code> for display of cuts. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 407: | Line 407: | ||
bail: fprintf(stderr, "bad args\n"); |
bail: fprintf(stderr, "bad args\n"); |
||
return 1; |
return 1; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="cpp">#include <array> |
||
#include <iostream> |
#include <iostream> |
||
#include <stack> |
#include <stack> |
||
Line 494: | Line 494: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[2, 2] |
<pre>[2, 2] |
||
Line 540: | Line 540: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Count only. |
Count only. |
||
< |
<syntaxhighlight lang="lisp">(defun cut-it (w h &optional (recur t)) |
||
(if (oddp (* w h)) (return-from cut-it 0)) |
(if (oddp (* w h)) (return-from cut-it 0)) |
||
(if (oddp h) (rotatef w h)) |
(if (oddp h) (rotatef w h)) |
||
Line 582: | Line 582: | ||
(loop for h from 1 to w do |
(loop for h from 1 to w do |
||
(if (evenp (* w h)) |
(if (evenp (* w h)) |
||
(format t "~d x ~d: ~d~%" w h (cut-it w h)))))</ |
(format t "~d x ~d: ~d~%" w h (cut-it w h)))))</syntaxhighlight>output<syntaxhighlight lang="text">2 x 1: 2 |
||
2 x 2: 2 |
2 x 2: 2 |
||
3 x 2: 3 |
3 x 2: 3 |
||
Line 611: | Line 611: | ||
9 x 4: 553 |
9 x 4: 553 |
||
9 x 6: 31721 |
9 x 6: 31721 |
||
9 x 8: 1812667</ |
9 x 8: 1812667</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="d">import core.stdc.stdio, core.stdc.stdlib, core.stdc.string, std.typecons; |
||
enum int[2][4] dir = [[0, -1], [-1, 0], [0, 1], [1, 0]]; |
enum int[2][4] dir = [[0, -1], [-1, 0], [0, 1], [1, 0]]; |
||
Line 691: | Line 691: | ||
if (!(x & 1) || !(y & 1)) |
if (!(x & 1) || !(y & 1)) |
||
printf("%d x %d: %llu\n", y, x, solve(y, x, true)); |
printf("%d x %d: %llu\n", y, x, solve(y, x, true)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 x 1: 1 |
<pre>2 x 1: 1 |
||
Line 737: | Line 737: | ||
{{libheader| System.SysUtils}} |
{{libheader| System.SysUtils}} |
||
{{Trans|C}} |
{{Trans|C}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Cut_a_rectangle; |
program Cut_a_rectangle; |
||
Line 839: | Line 839: | ||
writeln(format('%d x %d: %d', [y, x, solve(y, x, True)])); |
writeln(format('%d x %d: %d', [y, x, solve(y, x, True)])); |
||
Readln; |
Readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
See [[#C]] |
See [[#C]] |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
APPLICATION |
APPLICATION |
||
Line 880: | Line 880: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
GRID |
GRID |
||
Line 1,045: | Line 1,045: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
POINT |
POINT |
||
Line 1,094: | Line 1,094: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,142: | Line 1,142: | ||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
===Count only=== |
===Count only=== |
||
< |
<syntaxhighlight lang="elixir">import Integer |
||
defmodule Rectangle do |
defmodule Rectangle do |
||
Line 1,182: | Line 1,182: | ||
if is_even(w * h), do: IO.puts "#{w} x #{h}: #{Rectangle.cut_it(w, h)}" |
if is_even(w * h), do: IO.puts "#{w} x #{h}: #{Rectangle.cut_it(w, h)}" |
||
end) |
end) |
||
end)</ |
end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,220: | Line 1,220: | ||
===Show each of the cuts=== |
===Show each of the cuts=== |
||
{{works with|Elixir|1.2}} |
{{works with|Elixir|1.2}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Rectangle do |
||
def cut(h, w, disp\\true) when rem(h,2)==0 or rem(w,2)==0 do |
def cut(h, w, disp\\true) when rem(h,2)==0 or rem(w,2)==0 do |
||
limit = div(h * w, 2) |
limit = div(h * w, 2) |
||
Line 1,296: | Line 1,296: | ||
Rectangle.cut(2, 2) |> length |> IO.puts |
Rectangle.cut(2, 2) |> length |> IO.puts |
||
Rectangle.cut(3, 4) |> length |> IO.puts</ |
Rectangle.cut(3, 4) |> length |> IO.puts</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,379: | Line 1,379: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,460: | Line 1,460: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,507: | Line 1,507: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="groovy">class CutRectangle { |
||
private static int[][] dirs = [[0, -1], [-1, 0], [0, 1], [1, 0]] |
private static int[][] dirs = [[0, -1], [-1, 0], [0, 1], [1, 0]] |
||
Line 1,567: | Line 1,567: | ||
println() |
println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[2, 2] |
<pre>[2, 2] |
||
Line 1,615: | Line 1,615: | ||
Calculation of the cuts happens in the ST monad, using a mutable STVector and a mutable STRef. The program style is therefore very imperative. |
Calculation of the cuts happens in the ST monad, using a mutable STVector and a mutable STRef. The program style is therefore very imperative. |
||
The strictness annotations in the Env type are necessary; otherwise, unevaluated thunks of updates of "env" would pile up with each recursion, ending in a stack overflow. |
The strictness annotations in the Env type are necessary; otherwise, unevaluated thunks of updates of "env" would pile up with each recursion, ending in a stack overflow. |
||
< |
<syntaxhighlight lang="haskell">import qualified Data.Vector.Unboxed.Mutable as V |
||
import Data.STRef |
import Data.STRef |
||
import Control.Monad (forM_, when) |
import Control.Monad (forM_, when) |
||
Line 1,695: | Line 1,695: | ||
show x ++ " x " ++ show y ++ ": " ++ show (cut (x, y)))) |
show x ++ " x " ++ show y ++ ": " ++ show (cut (x, y)))) |
||
[ (x, y) | x <- [1..10], y <- [1..x] ] |
[ (x, y) | x <- [1..10], y <- [1..x] ] |
||
</syntaxhighlight> |
|||
</lang> |
|||
With GHC -O3 the run-time is about 39 times the D entry. |
With GHC -O3 the run-time is about 39 times the D entry. |
||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">init=: - {. 1: NB. initial state: 1 square choosen |
||
prop=: < {:,~2 ~:/\ ] NB. propagate: neighboring squares (vertically) |
prop=: < {:,~2 ~:/\ ] NB. propagate: neighboring squares (vertically) |
||
poss=: I.@,@(prop +. prop"1 +. prop&.|. +. prop&.|."1) |
poss=: I.@,@(prop +. prop"1 +. prop&.|. +. prop&.|."1) |
||
Line 1,706: | Line 1,706: | ||
N=: <:@-:@#@, NB. how many neighbors to add |
N=: <:@-:@#@, NB. how many neighbors to add |
||
step=: [: ~.@; <@(((= i.@$) +. ])"0 _~ keep)"2 |
step=: [: ~.@; <@(((= i.@$) +. ])"0 _~ keep)"2 |
||
all=: step^:N@init</ |
all=: step^:N@init</syntaxhighlight> |
||
In other words, starting with a boolean matrix with one true square in one corner, make a list of all false squares which neighbor a true square, and then make each of those neighbors true, independently (discarding duplicate matrices from the resulting sequence of boolean matrices), and repeat this N times where N is (total cells divided by two)-1. Then discard those matrices where inverting them (boolean not), then flipping on horizontal and vertical axis is not an identity. |
In other words, starting with a boolean matrix with one true square in one corner, make a list of all false squares which neighbor a true square, and then make each of those neighbors true, independently (discarding duplicate matrices from the resulting sequence of boolean matrices), and repeat this N times where N is (total cells divided by two)-1. Then discard those matrices where inverting them (boolean not), then flipping on horizontal and vertical axis is not an identity. |
||
Line 1,714: | Line 1,714: | ||
Example use: |
Example use: |
||
< |
<syntaxhighlight lang="j"> '.#' <"2@:{~ all 3 4 |
||
┌────┬────┬────┬────┬────┬────┬────┬────┬────┐ |
┌────┬────┬────┬────┬────┬────┬────┬────┬────┐ |
||
│.###│.###│..##│...#│...#│....│....│....│....│ |
│.###│.###│..##│...#│...#│....│....│....│....│ |
||
Line 1,738: | Line 1,738: | ||
│.##.#│.#..#│#..##│#.###│#####│###.#│##..#│#..#.│#.##.│####.│###..│##...│#....│ |
│.##.#│.#..#│#..##│#.###│#####│###.#│##..#│#..#.│#.##.│####.│###..│##...│#....│ |
||
│#####│#####│#####│#####│#####│#####│#####│#####│#####│#####│#####│#####│#####│ |
│#####│#####│#####│#####│#####│#####│#####│#####│#####│#####│#####│#####│#####│ |
||
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘</ |
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|7}} |
{{works with|Java|7}} |
||
< |
<syntaxhighlight lang="java">import java.util.*; |
||
public class CutRectangle { |
public class CutRectangle { |
||
Line 1,807: | Line 1,807: | ||
System.out.println(); |
System.out.println(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>[2, 2] |
<pre>[2, 2] |
||
Line 1,853: | Line 1,853: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="julia"> |
||
const count = [0] |
const count = [0] |
||
const dir = [[0, -1], [-1, 0], [0, 1], [1, 0]] |
const dir = [[0, -1], [-1, 0], [0, 1], [1, 0]] |
||
Line 1,922: | Line 1,922: | ||
runtest() |
runtest() |
||
</ |
</syntaxhighlight> {{output}} <pre> |
||
2 x 1: 1 |
2 x 1: 1 |
||
2 x 2: 2 |
2 x 2: 2 |
||
Line 1,967: | Line 1,967: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
object RectangleCutter { |
object RectangleCutter { |
||
Line 2,042: | Line 2,042: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,090: | Line 2,090: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="lua">function array1D(w, d) |
||
local t = {} |
local t = {} |
||
for i=1,w do |
for i=1,w do |
||
Line 2,194: | Line 2,194: | ||
cutRectangle(2, 2) |
cutRectangle(2, 2) |
||
cutRectangle(4, 3)</ |
cutRectangle(4, 3)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[2, 2] |
<pre>[2, 2] |
||
Line 2,239: | Line 2,239: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[CutRectangle] |
||
dirs = AngleVector /@ Most[Range[0, 2 Pi, Pi/2]]; |
dirs = AngleVector /@ Most[Range[0, 2 Pi, Pi/2]]; |
||
CutRectangle[nm : {n_, m_}] := Module[{start, stop, count, sols}, |
CutRectangle[nm : {n_, m_}] := Module[{start, stop, count, sols}, |
||
Line 2,304: | Line 2,304: | ||
{j, maxsize} |
{j, maxsize} |
||
]][[2, 1]]; |
]][[2, 1]]; |
||
Column[Row[{#1, " \[Times] ", #2, ": ", #3}] & @@@ sols]</ |
Column[Row[{#1, " \[Times] ", #2, ": ", #3}] & @@@ sols]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 * 1: 1 |
<pre>2 * 1: 1 |
||
Line 2,322: | Line 2,322: | ||
6 * 6: 1018</pre> |
6 * 6: 1018</pre> |
||
Solutions can be visualised using: |
Solutions can be visualised using: |
||
< |
<syntaxhighlight lang="mathematica">size = {4, 3}; |
||
cr = CutRectangle[size]; |
cr = CutRectangle[size]; |
||
Graphics[{Style[Rectangle[{0, 0}, size], FaceForm[], EdgeForm[Red]], Style[Arrow[#], Black], Style[Point[#], Black]}, ] & /@ cr["Solutions"]</ |
Graphics[{Style[Rectangle[{0, 0}, size], FaceForm[], EdgeForm[Red]], Style[Arrow[#], Black], Style[Point[#], Black]}, ] & /@ cr["Solutions"]</syntaxhighlight> |
||
Which outputs graphical objects for each solution. |
Which outputs graphical objects for each solution. |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
var |
var |
||
Line 2,404: | Line 2,404: | ||
for x in 1..y: |
for x in 1..y: |
||
if not odd(x) or not odd(y): |
if not odd(x) or not odd(y): |
||
echo &"{y:2d} x {x:2d}: {solve(y, x, true)}"</ |
echo &"{y:2d} x {x:2d}: {solve(y, x, true)}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,453: | Line 2,453: | ||
{{trans|C}} |
{{trans|C}} |
||
Output is identical to C's. |
Output is identical to C's. |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
my @grid = 0; |
my @grid = 0; |
||
Line 2,534: | Line 2,534: | ||
} |
} |
||
MAIN();</ |
MAIN();</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Using a completely different home-brewed algorithm, slightly sub-optimal as noted in the code. |
Using a completely different home-brewed algorithm, slightly sub-optimal as noted in the code. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #004080;">integer</span> <span style="color: #000000;">show</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- max number to show |
<span style="color: #004080;">integer</span> <span style="color: #000000;">show</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- max number to show |
||
Line 2,651: | Line 2,651: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<span style="color: #000080;font-style:italic;">--?elapsed(time()-t0)</span> |
<span style="color: #000080;font-style:italic;">--?elapsed(time()-t0)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
Includes two random grids |
Includes two random grids |
||
Line 2,728: | Line 2,728: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="python">def cut_it(h, w): |
||
dirs = ((1, 0), (-1, 0), (0, -1), (0, 1)) |
dirs = ((1, 0), (-1, 0), (0, -1), (0, 1)) |
||
if h % 2: h, w = w, h |
if h % 2: h, w = w, h |
||
Line 2,780: | Line 2,780: | ||
print "%d x %d: %d" % (w, h, cut_it(w, h)) |
print "%d x %d: %d" % (w, h, cut_it(w, h)) |
||
main()</ |
main()</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>2 x 1: 1 |
<pre>2 x 1: 1 |
||
Line 2,814: | Line 2,814: | ||
===Faster version=== |
===Faster version=== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="python">try: |
||
import psyco |
import psyco |
||
except ImportError: |
except ImportError: |
||
Line 2,883: | Line 2,883: | ||
print "%d x %d: %d" % (y, x, count_only(x, y)) |
print "%d x %d: %d" % (y, x, count_only(x, y)) |
||
main()</ |
main()</syntaxhighlight> |
||
The output is the same. |
The output is the same. |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 2,936: | Line 2,936: | ||
(newline) |
(newline) |
||
(cuts 4 3 #f) |
(cuts 4 3 #f) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,048: | Line 3,048: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{trans|C}} |
{{trans|C}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub solve($hh, $ww, $recurse) { |
||
my ($h, $w, $t, @grid) = $hh, $ww, 0; |
my ($h, $w, $t, @grid) = $hh, $ww, 0; |
||
state $cnt; |
state $cnt; |
||
Line 3,087: | Line 3,087: | ||
((1..9 X 1..9).grep:{ .[0] ≥ .[1] }).flat.map: -> $y, $x { |
((1..9 X 1..9).grep:{ .[0] ≥ .[1] }).flat.map: -> $y, $x { |
||
say "$y × $x: " ~ solve $y, $x, True unless $x +& 1 and $y +& 1; |
say "$y × $x: " ~ solve $y, $x, True unless $x +& 1 and $y +& 1; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 × 1: 1 |
<pre>2 × 1: 1 |
||
Line 3,122: | Line 3,122: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===idiomatic=== |
===idiomatic=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program cuts rectangles into two symmetric pieces, the rectangles are cut along */ |
||
/*────────────────────────────────────────────────── unit dimensions and may be rotated.*/ |
/*────────────────────────────────────────────────── unit dimensions and may be rotated.*/ |
||
numeric digits 20 /*be able to handle some big integers. */ |
numeric digits 20 /*be able to handle some big integers. */ |
||
Line 3,167: | Line 3,167: | ||
end /*j*/ |
end /*j*/ |
||
@.t= @.t - 1 |
@.t= @.t - 1 |
||
_= len - t; @._= @._ - 1; return</ |
_= len - t; @._= @._ - 1; return</syntaxhighlight> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
Line 3,227: | Line 3,227: | ||
Also, I've discovered a formula for calculating the number of cuts for even '''M''' when '''N''' is '''3'''. |
Also, I've discovered a formula for calculating the number of cuts for even '''M''' when '''N''' is '''3'''. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program cuts rectangles into two symmetric pieces, the rectangles are cut along */ |
||
/*────────────────────────────────────────────────── unit dimensions and may be rotated.*/ |
/*────────────────────────────────────────────────── unit dimensions and may be rotated.*/ |
||
numeric digits 40 /*be able to handle some big integers. */ |
numeric digits 40 /*be able to handle some big integers. */ |
||
Line 3,288: | Line 3,288: | ||
end |
end |
||
end /*j*/ |
end /*j*/ |
||
@.q= @.q - 1; _= len - q; @._= @._ - 1; return</ |
@.q= @.q - 1; _= len - q; @._= @._ - 1; return</syntaxhighlight> |
||
{{out|output|text= is the same as the idiomatic version (above).}} <br><br> |
{{out|output|text= is the same as the idiomatic version (above).}} <br><br> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="ruby">def cut_it(h, w) |
||
if h.odd? |
if h.odd? |
||
return 0 if w.odd? |
return 0 if w.odd? |
||
Line 3,332: | Line 3,332: | ||
puts "%d x %d: %d" % [w, h, cut_it(w, h)] if (w * h).even? |
puts "%d x %d: %d" % [w, h, cut_it(w, h)] if (w * h).even? |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,369: | Line 3,369: | ||
===Show each of the cuts=== |
===Show each of the cuts=== |
||
< |
<syntaxhighlight lang="ruby">class Rectangle |
||
DIRS = [[1, 0], [-1, 0], [0, -1], [0, 1]] |
DIRS = [[1, 0], [-1, 0], [0, -1], [0, 1]] |
||
def initialize(h, w) |
def initialize(h, w) |
||
Line 3,442: | Line 3,442: | ||
rec = Rectangle.new(3,4) |
rec = Rectangle.new(3,4) |
||
puts rec.cut.size</ |
puts rec.cut.size</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,525: | Line 3,525: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="rust"> |
||
fn cwalk(mut vis: &mut Vec<Vec<bool>>, count: &mut isize, w: usize, h: usize, y: usize, x: usize, d: usize) { |
fn cwalk(mut vis: &mut Vec<Vec<bool>>, count: &mut isize, w: usize, h: usize, y: usize, x: usize, d: usize) { |
||
if x == 0 || y == 0 || x == w || y == h { |
if x == 0 || y == 0 || x == w || y == h { |
||
Line 3,599: | Line 3,599: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
proc walk {y x} { |
proc walk {y x} { |
||
Line 3,684: | Line 3,684: | ||
} |
} |
||
} |
} |
||
}} 10</ |
}} 10</syntaxhighlight> |
||
Output is identical. |
Output is identical. |
||
Line 3,691: | Line 3,691: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
Last two are very slooow to emerge (just under 10 mins overall). |
Last two are very slooow to emerge (just under 10 mins overall). |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
var grid = [] |
var grid = [] |
||
Line 3,767: | Line 3,767: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,815: | Line 3,815: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="zkl">fcn cut_it(h,w){ |
||
if(h.isOdd){ |
if(h.isOdd){ |
||
if(w.isOdd) return(0); |
if(w.isOdd) return(0); |
||
Line 3,848: | Line 3,848: | ||
count + walk(h/2 - 1, w/2); |
count + walk(h/2 - 1, w/2); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Note the funkiness in walk: vm.pasteArgs. This is because zkl functions are unaware of their scope, so a closure is needed (when calling walk) to capture state (nxt, blen, grid, h, w). Rather than creating a closure object each call, that state is passed in the arg list. So, when doing recursion, that state needs to be restored to the stack (the compiler isn't smart enough to recognize this case). |
Note the funkiness in walk: vm.pasteArgs. This is because zkl functions are unaware of their scope, so a closure is needed (when calling walk) to capture state (nxt, blen, grid, h, w). Rather than creating a closure object each call, that state is passed in the arg list. So, when doing recursion, that state needs to be restored to the stack (the compiler isn't smart enough to recognize this case). |
||
< |
<syntaxhighlight lang="zkl">foreach w,h in ([1..9],[1..w]){ |
||
if((w*h).isEven) println("%d x %d: %d".fmt(w, h, cut_it(w,h))); |
if((w*h).isEven) println("%d x %d: %d".fmt(w, h, cut_it(w,h))); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Output is identical. |
Output is identical. |