Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Danilin without the GoTo's
#1
https://qb64phoenix.com/forum/showthread...3#pid40263

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

And see it works fine in QBJS without the GoTo's:


And pretty darn fast too!
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#2
(02-28-2026, 10:23 PM)bplus Wrote: https://qb64phoenix.com/forum/showthread...3#pid40263

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
Reply
#3
Hey!  That looks like some AMAZING code!  Big Grin
Reply
#4
(03-01-2026, 02:20 AM)CharlieJV Wrote:
(02-28-2026, 10:23 PM)bplus Wrote: https://qb64phoenix.com/forum/showthread...3#pid40263

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

Interesting work around Charlie!

I just learned QBJS doesn't like GoSub's either:


Attached Files Thumbnail(s)
   
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#5
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)
Reply
#6
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.

Smile these different ways of skinning the cat (or as ahenry3068 says, fellating a feline) are interesting.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#7
Here's a simplified version for you guys, which doesn't use a lot of indentation which seems to bother Charlie:
Code: (Select All)
t = Timer(0.001): Solve: Print Using "time: #.####"; Timer(0.001) - t
Sub Solve: For a = 0 To 9: For b = 0 To 9: For c = 0 To 9
                If a * b - c = 4 Then Step2 a, b, c
Next c, b, a: End Sub
Sub Step2 (a, b, c): For d = 0 To 9: For e = 0 To 9: For f = 0 To 9
                If d + e + f = 8 Then Step3 a, b, c, d, e, f
Next f, e, d: End Sub
Sub Step3 (a, b, c, d, e, f): For g = 0 To 9: For h = 0 To 9: For i = 0 To 9
                If g / h + i = 8 _AndAlso a / d + g = 9 _AndAlso b + e + h = 8 _AndAlso c + f - i = 6 Then Print a, b, c; Chr$(10); d, e, f; Chr$(10); g, h, i; Chr$(10); Chr$(10)
Next i, h, g: End Sub

And in only 10 lines of code to boot!

Should work as-is in QBJS, I'd think, with no changes needed.
Reply
#8
Nice!
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#9
(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.
Reply
#10
WTH? is going on??? One more try:
Quote:Should work as-is in QBJS, I'd think, with no changes needed.

QBJS didn't like Using (dumped) and _AndAlso swapped with AND
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star MazeBall & DAV & Danilin Solve of Labyrinth DANILIN 1 821 10-12-2024, 11:57 AM
Last Post: DANILIN

Forum Jump:


Users browsing this thread: 1 Guest(s)