2

I recently dug out an old book of mine, The Hawaiian Computer Mystery, published in 1985. There is a fragment of code in BASIC on page 81,

1 For N = 7 to 77
2 Print N, SQR(N) - INT (SQR [N] )
3 Next N
4 End

I can sort of see what it should do, but I can't get it to run. There's apparently an error in the second line, but I can't figure out what.

  • I believe Philip Petrov's answer is correct (the [ ] are wrong), but you should note which BASIC compiler you are using, which editor, and which operating system. All versions of BASIC were not the same. – Bryan Williams Jun 19 '20 at 17:47
  • It doesn't state in the book what computer is being used. It is likely an Apple II. Snakes appear on the screen, but it's implied these are a supernatural manifestation, not part of a program. – Aaron Gullison Jun 19 '20 at 18:27

1 Answers1

7

Assuming that you must find the digits after the decimal point of the square root of a number, than the issue is with the square brackets - they must be round. The following code:

1 For N = 7 to 77
2 Print N, SQR(N) - INT (SQR (N) )
3 Next N
4 End

(blank line in the end) will produce the following result:

7             .64575124
8             .8284271
9             0
10            .1622777
11            .31662488
12            .46410155
13            .60555124
14            .7416575
15            .87298346
16            0
17            1.23105526E-1
18            .2426405
19            .35889912
20            .47213602
21            .5825758
22            .69041586
23            .7958317
24            .89897966
25            0
26            .09901953
27            .19615221
28            .29150248
29            .38516474
30            .47722578
31            .5677643
32            .65685415
33            .7445626
34            .8309517
35            .91608
36            0
37            .08276272
38            .16441393
39            .24499798
40            .3245554
41            .40312433
42            .48074055
43            .5574384
44            .63324976
45            .7082038
46            .78233004
47            .8556547
48            .9282031
49            0
50            .07106781
51            .14142847
52            .21110249
53            .28010988
54            .34846926
55            .41619825
56            .483315
57            .54983425
58            .6157732
59            .68114567
60            .7459669
61            .8102498
62            .8740077
63            .93725395
64            0
65            6.2257767E-2
66            .1240387
67            .18535233
68            .24621105
69            .30662346
70            .36660004
71            .42614937
72            .485281
73            .5440035
74            .60232544
75            .6602545
76            .71779823
77            .77496433
Philip Petrov
  • 975
  • 4
  • 8
  • Thanks. I got the revised code to run at https://www.calormen.com/jsbasic/ Not sure why it was printed as that. – Aaron Gullison Jun 19 '20 at 18:31
  • 3
    @AaronGullison, it was probably printed like that because of a book editor. In most circumstances, (()) is not proper English punctuation and should be ([]) instead. So it's likely some human editor somewhere did not understand they were messing up a programming language syntax, lol – Bryan Williams Jun 19 '20 at 18:35
  • @BryanWilliams - it happened to me too. The editor changed lots of quoting and braces in my code and I had to revise everything once again :) – Philip Petrov Jun 20 '20 at 08:29