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

m
Line 2,793:
 
For interest, the following solution uses CoreFoundation (CF) strings.
<syntaxhighlight lang="futurebasic">local fn FourRings( low as long, high as long, unique as BOOL, show as BOOL )
{{output}}
long a, b, c, d, e, f, g
<pre style="height:10ex;">
long t, total = 0
aaa
long l = len(str(high))
bbb
ccc
if ( l < len(str(low)) ) then l = len(str(low))
ddd
eee
if ( show )
fff
for a = 97 to 103
</pre>
print space(l);fn StringWithCharacters( @a, 1 );
next
print
print @" ";fn StringByPaddingToLength( @"", ( l + 1 ) * 7, @"-", 0 )
end if
for a = low to high
for b = low to high
if ( unique )
if ( b == a ) then continue
end if
t = a + b
for c = low to high
if ( unique )
if ( c == a or c == b ) then continue
end if
for d = low to high
if ( unique )
if ( d == a or d == b or d == c ) then continue
end if
if ( b + c + d == t )
for e = low to high
if ( unique )
if ( e == a or e == b or e == c or e == d ) then continue
end if
for f = low to high
if ( unique )
if ( f == a or f == b or f == c or f == d or f == e ) then continue
end if
if ( d + e + f == t )
for g = low to high
if ( unique )
if ( g == a or g == b or g == c or g == d or g == e or g == f ) then continue
end if
if ( f + g == t )
total += 1
if ( show )
printf @"%3d%3d%3d%3d%3d%3d%3d", a, b, c, d, e, f, g
end if
end if
next
end if
next
next
end if
next
next
next
next
if ( unique )
print
print total;@" unique solutions for ";low;@" to ";high
print fn StringByPaddingToLength( @"", 30, @"-", 0 )
print
else
print total;@" non-unique solutions for ";low;@" to ";high
print fn StringByPaddingToLength( @"", 37, @"-", 0 )
print
end if
end fn
 
window 1, @"4 Rings", ( 0, 0, 350, 400 )
 
fn FourRings( 1, 7, YES, YES )
fn FourRings( 3, 9, YES, YES )
fn FourRings( 0, 9, NO, NO )
 
HandleEvents</syntaxhighlight>
 
{{output}}
408

edits