Skip to content

Instantly share code, notes, and snippets.

@timo
Created June 3, 2018 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timo/0070857a2f44a071169eef2d787c2cb9 to your computer and use it in GitHub Desktop.
Save timo/0070857a2f44a071169eef2d787c2cb9 to your computer and use it in GitHub Desktop.
nqp and perl6 optimized versions of shlomi fish's (rindolf's) euler 287 solution.
#!/usr/bin/env perl6
# The Expat License
#
# Copyright (c) 2018, Shlomi Fish
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
use nqp;
my int $N = 24;
my int $E = 1 +< ($N-1);
my int $E2 = $E*$E;
my int $M = (($E +< 1) - 1);
sub probe(int $x, int $y)
{
nqp::isle_i(
nqp::add_i(
nqp::pow_i(nqp::sub_i($x, $E), 2),
nqp::pow_i(nqp::sub_i($y, $E), 2)),
$E2);
}
sub Len(int \x, int \y, int \w)
{
nqp::if(
nqp::iseq_i(w, 1),
2,
nqp::stmts(
(my int $x2 = nqp::sub_i(nqp::add_i(x, w), 1)),
(my int $y2 = nqp::sub_i(nqp::add_i(y, w), 1)),
(my \p1 = probe(x, y)),
(my \p2 = probe(x, $y2)),
(my \p3 = nqp::if(
(p1 == p2),
(probe($x2, $y2)),
(-1))),
(my \p4 = nqp::if(
(p2 == p3),
(probe($x2, y)),
(-1))),
nqp::if((p3 == p4 && p4 != -1),
2,
split_len(x, y, w))
)
)
}
sub split_len(int $x, int $y, int $w) {
my int $w2 = nqp::bitshiftr_i($w, 1);
my int $xm = nqp::add_i($x, $w2);
my int $ym = nqp::add_i($y, $w2);
1 + Len($x, $y, $w2) + Len($xm, $y, $w2) + Len($x, $ym, $w2) + Len($xm, $ym, $w2);
}
given split_len(0, 0, $M+1) {
.say;
}
#!/usr/bin/env perl6
# The Expat License
#
# Copyright (c) 2018, Shlomi Fish
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
my int $N := 24;
my int $E := nqp::bitshiftl_i(1, ($N-1));
my int $E2 := $E*$E;
my int $M := (nqp::bitshiftl_i($E, 1) - 1);
sub probe(int $x, int $y)
{
nqp::isle_i(
nqp::add_i(
nqp::pow_i(nqp::sub_i($x, $E), 2),
nqp::pow_i(nqp::sub_i($y, $E), 2)),
$E2);
}
sub Len(int $x, int $y, int $w)
{
nqp::if(
nqp::iseq_i(w, 1),
2,
nqp::stmts(
(my int $x2 := nqp::sub_i(nqp::add_i($x, $w), 1)),
(my int $y2 := nqp::sub_i(nqp::add_i($y, $w), 1)),
(my $p1 := probe($x, $y)),
(my $p2 := probe($x, $y2)),
(my $p3 := nqp::if(
($p1 == $p2),
(probe($x2, $y2)),
(-1))),
(my $p4 := nqp::if(
($p2 == $p3),
(probe($x2, $y)),
(-1))),
nqp::if(($p3 == $p4 && $p4 != -1),
2,
split_len($x, $y, $w))
)
)
}
sub split_len(int $x, int $y, int $w) {
my int $w2 := nqp::bitshiftr_i($w, 1);
my int $xm := nqp::add_i($x, $w2);
my int $ym := nqp::add_i($y, $w2);
1 + Len($x, $y, $w2) + Len($xm, $y, $w2) + Len($x, $ym, $w2) + Len($xm, $ym, $w2);
}
say(split_len(0, 0, $M+1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment