Magic squares of odd order: Difference between revisions
Content added Content deleted
(Added solution for EDSAC) |
(Added various BASIC dialects (Chipmunk Basic, GW-BASIC and MSX Basic)) |
||
Line 877: | Line 877: | ||
20 11 7 3 24 |
20 11 7 3 24 |
||
MAGIC CONSTANT: 65</pre> |
MAGIC CONSTANT: 65</pre> |
||
==={{header|Chipmunk Basic}}=== |
|||
{{works with|Chipmunk Basic|3.6.4}} |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="qbasic">100 cls |
|||
110 sub magicsq(size,filename$ = "") |
|||
120 if (size and 1) = 0 or size < 3 then |
|||
130 print |
|||
140 print "error: size is not odd or size is smaller then 3" |
|||
160 exit sub |
|||
170 endif |
|||
180 ' filename$ <> "" then save magic square in a file |
|||
190 ' filename$ can contain directory name |
|||
200 ' if filename$ exist it will be overwriten, no error checking |
|||
210 dim sq(size,size)' array to hold square |
|||
220 ' start in the middle of the first row |
|||
230 nr = 1 |
|||
240 x = size-int(size/2) |
|||
250 y = 1 |
|||
260 max = size*size |
|||
270 ' create format string for using |
|||
280 for c = 1 to len(str$(max))+1 : frmt$ = frmt$+"#" : next c |
|||
290 'main loop for creating magic square |
|||
300 do |
|||
310 if sq(x,y) = 0 then |
|||
320 sq(x,y) = nr |
|||
330 if nr mod size = 0 then |
|||
340 y = y+1 |
|||
350 else |
|||
360 x = x+1 |
|||
370 y = y-1 |
|||
380 endif |
|||
390 nr = nr+1 |
|||
400 endif |
|||
410 if x > size then |
|||
420 x = 1 |
|||
430 do while sq(x,y) <> 0 |
|||
440 x = x+1 |
|||
450 loop |
|||
460 endif |
|||
470 if y < 1 then |
|||
480 y = size |
|||
490 do while sq(x,y) <> 0 |
|||
500 y = y-1 |
|||
510 loop |
|||
520 endif |
|||
530 loop until nr > max |
|||
540 ' printing square's bigger than 19 result in a wrapping of the line |
|||
550 print "Odd magic square size: ";size;"*";size |
|||
560 print "The magic sum = ";int((max+1)/2)*size |
|||
570 print |
|||
580 for y = 1 to size |
|||
590 for x = 1 to size |
|||
600 print using "####";val(sq(x,y)); |
|||
610 next x |
|||
620 print |
|||
630 next y |
|||
640 print |
|||
650 ' output magic square to a file with the name provided |
|||
660 if filename$ <> "" then |
|||
670 nr = freefile |
|||
680 open filename$ for output as #1 |
|||
690 print #1,"Odd magic square size: ";size;"*";size |
|||
700 print #1,"The magic sum = ";int((max+1)/2)*size |
|||
710 print #1, |
|||
720 for y = 1 to size |
|||
730 for x = 1 to size |
|||
740 print #1,using frmt$;sq(x,y); |
|||
750 next x |
|||
760 print #1, |
|||
770 next y |
|||
780 endif |
|||
790 close #1 |
|||
800 end sub |
|||
810 input "Enter N: ",number |
|||
820 magicsq(number) |
|||
830 end</syntaxhighlight> |
|||
==={{header|GW-BASIC}}=== |
|||
{{works with|PC-BASIC|any}} |
|||
{{works with|BASICA}} |
|||
{{works with|Chipmunk Basic}} |
|||
{{works with|QBasic}} |
|||
{{works with|MSX BASIC}} |
|||
{{trans|IS-BASIC}} |
|||
<syntaxhighlight lang="qbasic">100 REM Magic squares of odd order |
|||
110 INPUT "The square order: ", N |
|||
115 'INPUT "The square order:"; N ' for MSX Basic |
|||
120 IF (N AND 1) = 0 OR N < 3 THEN PRINT "error: size is not odd or size is smaller then 3" : GOTO 100 |
|||
130 FOR I = 1 TO N |
|||
140 FOR J = 1 TO N |
|||
150 PRINT USING " ###"; ((I*2-J+N-1) MOD N) * N + ((I*2+J-2) MOD N) + 1; |
|||
160 NEXT J |
|||
170 PRINT |
|||
180 NEXT I |
|||
190 PRINT "The magic number is: "; N * (N^2+1) / 2</syntaxhighlight> |
|||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
Line 890: | Line 986: | ||
190 NEXT |
190 NEXT |
||
200 PRINT "The magic number is:";N*(N^2+1)/2</syntaxhighlight> |
200 PRINT "The magic number is:";N*(N^2+1)/2</syntaxhighlight> |
||
==={{header|MSX Basic}}=== |
|||
{{works with|MSX BASIC|any}} |
|||
{{trans|IS-BASIC}} |
|||
<syntaxhighlight lang="qbasic">100 REM Magic squares of odd order |
|||
110 INPUT "The square order:"; N |
|||
120 IF (N AND 1) = 0 OR N < 3 THEN PRINT "error: size is not odd or size is smaller then 3" : GOTO 100 |
|||
130 FOR I = 1 TO N |
|||
140 FOR J = 1 TO N |
|||
150 PRINT USING " ###"; ((I*2-J+N-1) MOD N) * N + ((I*2+J-2) MOD N) + 1; |
|||
160 NEXT J |
|||
170 PRINT |
|||
180 NEXT I |
|||
190 PRINT "The magic number is:"; N * (N^2+1) / 2</syntaxhighlight> |
|||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |