Here is how Danilin could have written the above code (2nd one in reply) without the goto's:
Code: (Select All)
Dim t: t = Timer(0.001) ' ussr_puzzle_subzero.bas
For a = 0 To 9: For b = 0 To 9: For c = 0 To 9
If a * b - c = 4 Then
For d = 1 To 9: For e = 0 To 9: For f = 0 To 9
If d + e + f = 8 Then
For g = 0 To 9: For h = 1 To 9: For i = 0 To 9
If g / h + i = 8 Then
If a / d + g = 9 Then
If b + e + h = 8 Then
If c + f - i = 6 Then
Print a, b, c
Print d, e, f
Print g, h, i: Print
End If
End If
End If
End If
Next i
Next h
Next g
End If
Next f
Next e
Next d
End If
Next c
Next b
Next a
Print "time:"; Timer(0.001) - t
Here is how Danilin could have written the above code (2nd one in reply) without the goto's:
...snip...
Having blurry double-vision, I have a hard time with a lot of indentation.
I find this easier to read:
Code: (Select All)
Dim t: t = Timer(0.001) ' ussr_puzzle_subzero.bas
For a = 0 To 9: For b = 0 To 9: For c = 0 To 9
If a * b - c = 4 Then GOSUB For_d_e_f
Next c, b, a
Print "time:"; Timer(0.001) - t
END
For_d_e_f:
For d = 1 To 9: For e = 0 To 9: For f = 0 To 9
If d + e + f = 8 Then GOSUB For_g_h_i
Next f, e, d
RETURN
For_g_h_i:
For g = 0 To 9: For h = 1 To 9: For i = 0 To 9
If g / h + i = 8 Then
If a / d + g = 9 Then
If b + e + h = 8 Then
If c + f - i = 6 Then
Print a, b, c
Print d, e, f
Print g, h, i: Print
End If : End If : End If : End If
Next i, h, g
RETURN
Here is how Danilin could have written the above code (2nd one in reply) without the goto's:
...snip...
Having blurry double-vision, I have a hard time with a lot of indentation.
I find this easier to read:
Code: (Select All)
Dim t: t = Timer(0.001) ' ussr_puzzle_subzero.bas
For a = 0 To 9: For b = 0 To 9: For c = 0 To 9
If a * b - c = 4 Then GOSUB For_d_e_f
Next c, b, a
Print "time:"; Timer(0.001) - t
END
For_d_e_f:
For d = 1 To 9: For e = 0 To 9: For f = 0 To 9
If d + e + f = 8 Then GOSUB For_g_h_i
Next f, e, d
RETURN
For_g_h_i:
For g = 0 To 9: For h = 1 To 9: For i = 0 To 9
If g / h + i = 8 Then
If a / d + g = 9 Then
If b + e + h = 8 Then
If c + f - i = 6 Then
Print a, b, c
Print d, e, f
Print g, h, i: Print
End If : End If : End If : End If
Next i, h, g
RETURN
03-01-2026, 11:23 AM (This post was last modified: 03-01-2026, 11:24 AM by luke.)
The original code was too simple and indented so I took the liberty of complexifying it for you:
Code: (Select All)
$Checking:Off
Const NUM_VALUES = 10
Type vec3
a As Integer
b As Integer
c As Integer
End Type
Function next_vec3 (v As vec3)
next_vec3 = _TRUE
v.c = (v.c + 1) Mod NUM_VALUES
If v.c = 0 Then
v.b = (v.b + 1) Mod NUM_VALUES
If v.b = 0 Then
v.a = (v.a + 1) Mod NUM_VALUES
next_vec3 = v.a <> 0
End If
End If
End Function
Function next_muldiff (v As vec3, target As Integer)
Do
If next_vec3(v) = 0 Then Exit Function
If v.a * v.b - v.c = target Then
next_muldiff = _TRUE
Exit Function
End If
Loop
End Function
Function next_sum (v As vec3, target As Integer)
Do
If next_vec3(v) = 0 Then Exit Function
If v.a + v.b + v.c = target Then
next_sum = _TRUE
Exit Function
End If
Loop
End Function
Function next_divsum (v As vec3, target As Integer)
Do
If next_vec3(v) = 0 Then Exit Function
If v.a / v.b + v.c = target Then
next_divsum = _TRUE
Exit Function
End If
Loop
End Function
Sub zero_vec3 (v As vec3)
v.a = 0
v.b = 0
v.c = 0
End Sub
Sub print_vec3 (v As vec3)
Print v.a; v.b; v.c
End Sub
Dim As vec3 x, y, z
t# = Timer(0.001)
Do While next_muldiff(x, 4)
zero_vec3 y
y.a = 1
Do While next_sum(y, 8)
zero_vec3 z
z.b = 1
Do While next_divsum(z, 8)
If x.a / y.a + z.a = 9 _AndAlso _
x.b + y.b + z.b = 8 _AndAlso _
x.c + y.c - z.c = 6 Then
print_vec3 x
print_vec3 y
print_vec3 z
Print
End If
Loop
Loop
Loop
Print Timer(0.001) - t#
It's only about 7 times slower than b+'s original (with C++ optimizations turned on)
03-01-2026, 02:00 PM (This post was last modified: 03-01-2026, 02:16 PM by bplus.)
Quote:It's only about 7 times slower than b+'s original (with C++ optimizations turned on)
$Checking: Off saves maybe .005 secs
I thought it might be the UDT so I tried this:
Code: (Select All)
'$Checking:Off
Const NUM_VALUES = 10
'Type vec3
' a As Integer
' b As Integer
' c As Integer
'End Type
'Dim As vec3 x, y, z
Dim As Integer xa, xb, xc, ya, yb, yc, za, zb, zc
t# = Timer(0.001)
Do While next_muldiff(xa, xb, xc, 4)
zero_vec3 ya, yb, yc
ya = 1
Do While next_sum(ya, yb, yc, 8)
zero_vec3 za, zb, zc
zb = 1
Do While next_divsum(za, zb, zc, 8)
If xa / ya + za = 9 _AndAlso _
xb + yb + zb = 8 _AndAlso _
xc + yc - zc = 6 Then
print_vec3 xa, xb, xc
print_vec3 ya, yb, yc
print_vec3 za, zb, zc
Print
End If
Loop
Loop
Loop
Print Timer(0.001) - t#
Function next_vec3 (a As Integer, b As Integer, c As Integer)
next_vec3 = _TRUE
c = (c + 1) Mod NUM_VALUES
If c = 0 Then
b = (b + 1) Mod NUM_VALUES
If b = 0 Then
a = (a + 1) Mod NUM_VALUES
next_vec3 = a <> 0
End If
End If
End Function
Function next_muldiff (a As Integer, b As Integer, c As Integer, target As Integer)
Do
If next_vec3(a, b, c) = 0 Then Exit Function
If a * b - c = target Then
next_muldiff = _TRUE
Exit Function
End If
Loop
End Function
Function next_sum (a As Integer, b As Integer, c As Integer, target As Integer)
Do
If next_vec3(a, b, c) = 0 Then Exit Function
If a + b + c = target Then
next_sum = _TRUE
Exit Function
End If
Loop
End Function
Function next_divsum (a As Integer, b As Integer, c As Integer, target As Integer)
Do
If next_vec3(a, b, c) = 0 Then Exit Function
If a / b + c = target Then
next_divsum = _TRUE
Exit Function
End If
Loop
End Function
Sub zero_vec3 (a As Integer, b As Integer, c As Integer)
a = 0
b = 0
c = 0
End Sub
Sub print_vec3 (a As Integer, b As Integer, c As Integer)
Print a; b; c
End Sub
but no big change in times.
Quote:... so I took the liberty of complexifying it for you.
these different ways of skinning the cat (or as ahenry3068 says, fellating a feline) are interesting.
03-01-2026, 06:37 PM (This post was last modified: 03-01-2026, 06:38 PM by CharlieJV.)
(03-01-2026, 03:19 PM)SMcNeill Wrote: ... which seems to bother Charlie...
Hey, apologies if I come across as a complainer.
I've got an arachnoid cyst that is taking up about 10-15% of the space in my cranium, squishing my brain and causing a degree of double-vision and messing with my ability to function in general.
Fuch's dystrophy, which makes vision blurry on top of the double-vision.
Yes, I do get very bothered when trying to see/understand what I'm looking at is screwed up by "sticks in my wheels" and "wheels stuck in the mud".
I figure as "out in left field" my way of doing things is most (all?) of the time, maybe once in a blue moon my approach from the crazy train might inspire, for the one other oddball out there, some creative juices flowing for some related or entirely unrelated thing.