4-rings or 4-squares puzzle: Difference between revisions

No edit summary
Line 3,822:
 
2860 non-unique solutions found using: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9</pre>
===With Recursion===
<lang perl>#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/4-rings_or_4-squares_puzzle
use warnings;
 
for ( [1 .. 7], [3 .. 9] )
{
print "for @$_\n\n";
findunique( $_ );
print "\n";
}
my $count = 0;
findcount();
print "count of non-unique 0-9: $count\n";
 
sub findunique
{
my @allowed = @{ shift @_ };
if( @_ == 4 ) { $_[0] == $_[2] + $_[3] or return }
elsif( @_ == 6 ) { $_[1] + $_[2] == $_[4] + $_[5] or return }
elsif( @_ == 7 ) { $_[3] + $_[4] == $_[6] and print "@_\n"; return }
for my $n ( @allowed )
{
findunique( [ grep $n != $_, @allowed ], @_, $n );
}
}
 
sub findcount
{
if( @_ == 4 ) { $_[0] == $_[2] + $_[3] or return }
elsif( @_ == 6 ) { $_[1] + $_[2] == $_[4] + $_[5] or return }
elsif( @_ == 7 ) { $_[3] + $_[4] == $_[6] and $count++; return }
findcount( @_, $_ ) for 0 .. 9;
}</lang>
{{out}}
<pre>
for 1 2 3 4 5 6 7
 
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
 
for 3 4 5 6 7 8 9
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
 
count of non-unique 0-9: 2860
</pre>
 
=={{header|Phix}}==
Anonymous user