24 game/Solve: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: future-proof for 5.36, explicit :prototype) |
No edit summary |
||
Line 4,143: | Line 4,143: | ||
2 4 6 8 : (( 2 / 4 )* 6 )* 8 |
2 4 6 8 : (( 2 / 4 )* 6 )* 8 |
||
</pre> |
</pre> |
||
=={{header|FutureBasic}}== |
|||
This function gives just the first found (so simplest) solution. To see the exhaustive list, we would just remove the exit fn statements. |
|||
<syntaxhighlight lang="futurebasic> |
|||
begin globals |
|||
Short k |
|||
end globals |
|||
void local fn eval( t as CFStringRef ) |
|||
CFMutableStringRef s = fn MutableStringNew |
|||
ExpressionRef x = fn ExpressionWithFormat( t ) |
|||
CFRange r = fn CFRangeMake(0, fn StringLength( t ) ) |
|||
CFNumberRef n = fn ExpressionValueWithObject( x, Null, Null ) |
|||
Float f = dblval( n ) |
|||
if f = 24 // found, so clean up |
|||
MutableStringSetString( s, t ) // duplicate string and pretend it was integers all along |
|||
MutableStringReplaceOccurrencesOfString( s, @".000000", @"", Null, r ) |
|||
print s; @" = 24" : k ++ |
|||
end if |
|||
end fn |
|||
clear local fn work( t as CFStringRef ) |
|||
Short a, b, c, d, e, f, g |
|||
CGFloat n(3) |
|||
CFStringRef s, os = @"*/+-", o(3) |
|||
print t, : k = 0 |
|||
// Put digits (as floats) and operators (as strings) in arrays |
|||
for a = 0 to 3 : s = mid( t, a, 1 ) : n(a) = fn StringFloatValue( s ) : o(a) = mid( os, a, 1 ) : next |
|||
// Permutions for the digits ... |
|||
for d = 0 to 3 : for e = 0 to 3 : for f = 0 to 3 : for g = 0 to 3 |
|||
if d != e and d != f and d != g and e != f and e != g and f != g // ... without duplications |
|||
// Combinations for the operators (3 from 4, with replacement) |
|||
for a = 0 to 3 : for b = 0 to 3 : for c = 0 to 3 |
|||
fn eval( fn StringWithFormat( @"%f %@ %f %@ %f %@ %f", n(d), o(a), n(e), o(b), n(f), o(c), n(g) ) ) : if k > 0 then exit fn |
|||
fn eval( fn StringWithFormat( @"%f %@ ( %f %@ %f ) %@ %f", n(d), o(a), n(e), o(b), n(f), o(c), n(g) ) ) : if k > 0 then exit fn |
|||
fn eval( fn StringWithFormat( @"%f %@ %f %@ ( %f %@ %f )", n(d), o(a), n(e), o(b), n(f), o(c), n(g) ) ) : if k > 0 then exit fn |
|||
fn eval( fn StringWithFormat( @"%f %@ ( %f %@ %f %@ %f )", n(d), o(a), n(e), o(b), n(f), o(c), n(g) ) ) : if k > 0 then exit fn |
|||
fn eval( fn StringWithFormat( @"( %f %@ %f ) %@ %f %@ %f", n(d), o(a), n(e), o(b), n(f), o(c), n(g) ) ) : if k > 0 then exit fn |
|||
fn eval( fn StringWithFormat( @"( %f %@ %f %@ %f ) %@ %f", n(d), o(a), n(e), o(b), n(f), o(c), n(g) ) ) : if k > 0 then exit fn |
|||
fn eval( fn StringWithFormat( @"%f %@ ( %f %@ ( %f %@ %f ) )", n(d), o(a), n(e), o(b), n(f), o(c), n(g) ) ) : if k > 0 then exit fn |
|||
fn eval( fn StringWithFormat( @"( %f %@ %f ) %@ ( %f %@ %f )", n(d), o(a), n(e), o(b), n(f), o(c), n(g) ) ) : if k > 0 then exit fn |
|||
fn eval( fn StringWithFormat( @"( %f %@ ( %f %@ %f )) %@ %f", n(d), o(a), n(e), o(b), n(f), o(c), n(g) ) ) : if k > 0 then exit fn |
|||
fn eval( fn StringWithFormat( @"( ( %f %@ %f ) %@ %f ) %@ %f", n(d), o(a), n(e), o(b), n(f), o(c), n(g) ) ) : if k > 0 then exit fn |
|||
fn eval( fn StringWithFormat( @"%f %@ ( ( %f %@ %f ) %@ %f )", n(d), o(a), n(e), o(b), n(f), o(c), n(g) ) ) : if k > 0 then exit fn |
|||
next : next : next |
|||
end if |
|||
next : next : next : next |
|||
end fn |
|||
window 1, @"24 Game", ( 0, 0, 250, 250 ) |
|||
fn work(@"3388") |
|||
fn work(@"1346") |
|||
fn work(@"8752") |
|||
handleevents |
|||
</syntaxhighlight> |
|||
=={{header|GAP}}== |
=={{header|GAP}}== |