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

Content added Content deleted
(→‎{{header|Fortran}}: If you can't say anything good...)
Line 712: Line 712:
</pre>
</pre>
=={{header|Fortran}}==
=={{header|Fortran}}==
This uses the facility standardised in F90 whereby DO-loops can have text labels attached (not in the usual label area) so that the END DO statement can have the corresponding label, and any CYCLE statements can use it also. Similarly, the subroutine's END statement bears the name of the subroutine. This is just syntactic decoration. Rather more useful is extended syntax for dealing with arrays and especially the function ANY for making multiple tests without having to enumerate them in the code. To gain this convenience, the EQUIVALENCE statement makes variables A, B, C, D, E, F, and G occupy the same storage as <code>INTEGER V(7)</code>, an array.
This uses the facility standardised in F90 whereby DO-loops can have text labels attached (not in the usual label area) so that the END DO statement can have the corresponding label, and any CYCLE statements can use it also. Similarly, the subroutine's END statement bears the name of the subroutine. This is just syntactic decoration. Rather more useful is extended syntax for dealing with arrays and especially the function ANY for making multiple tests without having to enumerate them in the code. To gain this convenience, the EQUIVALENCE statement makes variables A, B, C, D, E, F, and G occupy the same storage as <code>INTEGER V(7)</code>, an array.


One could abandon the use of the named variables in favour of manipulating the array equivalent, and indeed develop code which performs the nested loops via messing with the array, but for simplicity, the individual variables are used. However, tempting though it is to write a systematic sequence of seven nested DO-loops, the variables are not in fact all independent: some are fixed once others are chosen. Just cycling through all the notional possibilities when one only is in fact possible is a bit too much brute-force-and-ignorance, though other problems with other constraints, may encourage such exhaustive stepping. As a result, the code is more tightly bound to the specific features of the problem.
One could abandon the use of the named variables in favour of manipulating the array equivalent, and indeed develop code which performs the nested loops via messing with the array, but for simplicity, the individual variables are used. However, tempting though it is to write a systematic sequence of seven nested DO-loops, the variables are not in fact all independent: some are fixed once others are chosen. Just cycling through all the notional possibilities when one only is in fact possible is a bit too much brute-force-and-ignorance, though other problems with other constraints, may encourage such exhaustive stepping. As a result, the code is more tightly bound to the specific features of the problem.
Line 789: Line 789:
2860 found.
2860 found.
</pre>
</pre>

One might hope that the ANY function will quit as soon as possible and that it will not be invoked if UNIQUE is false, but the modernisers have rejected reliance on [[Talk:Short-circuit_evaluation#Compiler_optimisations.3F|short-circuit evaluation]] and the "help" is quite general on the workings of the ANY function, as also is modern. Here is a sample of the code produced by the Compaq 6.6a Visual Fortran F90/95 compiler, in its normal "debugging" condition and array bound checking of course active...
<pre>
31: IF (UNIQUE .AND. ANY(V(1:6).EQ.G)) CYCLE EE !And, if required, unique?
00401496 mov edi,dword ptr [UNIQUE]
00401499 mov edi,dword ptr [edi]
0040149B mov ebx,dword ptr [G (00470380)]
004014A1 mov eax,0
004014A6 mov ecx,1
004014AB mov dword ptr [ebp-60h],1
004014B2 cmp dword ptr [ebp-60h],6
004014B6 jg FOURSHOW+4C4h (004014fc)
004014B8 cmp ecx,1
004014BB jl FOURSHOW+48Ah (004014c2)
004014BD cmp ecx,7
004014C0 jle FOURSHOW+493h (004014cb)
004014C2 xor esi,esi
004014C4 mov dword ptr [ebp-6Ch],esi
004014C7 dec esi
004014C8 bound esi,qword ptr [ebp-6Ch]
004014CB imul esi,ecx,4
004014CE mov esi,dword ptr S+4 (00470364)[esi]
004014D4 xor edx,edx
004014D6 cmp esi,ebx
004014D8 sete dl
004014DB mov dword ptr [ebp-6Ch],edx
004014DE mov edx,eax
004014E0 or edx,dword ptr [ebp-6Ch]
004014E3 and edx,1
004014E6 mov eax,edx
004014E8 neg eax
004014EA mov esi,ecx
004014EC add esi,1
004014EF mov ecx,esi
004014F1 mov edx,dword ptr [ebp-60h]
004014F4 add edx,1
004014F7 mov dword ptr [ebp-60h],edx
004014FA jmp FOURSHOW+47Ah (004014b2)
004014FC and edi,eax
004014FE mov edx,edi
00401500 and edx,1
00401503 cmp edx,0
00401506 jne FOURSHOW+531h (00401569)
32: N = N + 1 !Yes! Count a solution set!
00401508 mov esi,dword ptr [N (0047035c)]
0040150E add esi,1
00401511 mov dword ptr [N (0047035c)],esi
33: IF (UNIQUE) WRITE (6,"(7I3)") V !Show its values.
</pre>
I'd rather say nothing at all.


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==