What can I do to run this BASIC Program? - basic

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.

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

Related

printing a string like a matrix

Trying to let the user input a number, and print a table according to the square of its size. Here's an example.
Size--> 3
0 1 2
3 4 5
6 7 8
Size--> 4
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Size--> 6
0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
18 19 20 21 22 23
24 25 26 27 28 29
30 31 32 33 34 35
Size--> 9
0 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44
45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62
63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80
Here's is the code that i have tried.
length=int(input('Size--> '))
size=length*length
biglist=[]
for i in range(size):
biglist.append(i)
biglist = [str(i) for i in biglist]
for i in range(0, len(biglist), length):
print(' '.join(biglist[i: i+length]))
but instead here's what i got
Size--> 3
0 1 2
3 4 5
6 7 8
Size--> 4
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Size--> 6
0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
18 19 20 21 22 23
24 25 26 27 28 29
30 31 32 33 34 35
As you can see the rows are not aligned properly like the example.
What's the simplest way of presenting it in a proper alignment? Thx :)
Using .format on string with right aligning.
And strlen is the number of characters required for each number.
length = int(input('Size--> '))
size = length*length
biglist = []
for i in range(size):
biglist.append(i)
biglist = [str(i) for i in biglist]
strlen = len(str(length**2-1))+1
for i in range(0, len(biglist), length):
# print(' '.join(biglist[i: i+length]))
for x in biglist[i: i+length]:
print(f"{x:>{strlen}}", end='')
print()

Reading in multidigit command line parameter

I'm learning J and have modified a tutorial into a jconsole script invoked by ./knight.j N to return as output the Knight's tour for an NxN board.
#!/usr/local/bin/j
kmoves=: 3 : 0
t=. (>,{;~i.y) +"1/ _2]\2 1 2 _1 1 2 1 _2 _1 2 _1 _2 _2 1 _2 _1
(*./"1 t e. i.y) <##"1 y#.t
)
ktour=: 3 : 0
M=. >kmoves y
p=. k=. 0
b=. 1 $~ *:y
for. i.<:*:y do.
b=. 0 k}b
p=. p,k=. ((i.<./) +/"1 b{~j{M){j=. ({&b # ]) k{M
end.
assert. ~:p
(,~y)$/:p
)
echo ktour 0".>2}.ARGV
exit''
However, I'm having difficulty in handling ARGV for numbers greater than 9. The script works correctly with single digit input:
$ ./knight.j 8
0 25 14 23 28 49 12 31
15 22 27 50 13 30 63 48
26 1 24 29 62 59 32 11
21 16 51 58 43 56 47 60
2 41 20 55 52 61 10 33
17 38 53 42 57 44 7 46
40 3 36 19 54 5 34 9
37 18 39 4 35 8 45 6
But fails on double digit input:
$ ./knight.j 10
|length error: kmoves
| (*./"1 t e.i.y)<##"1 y #.t
ARGV
┌─────────────────┬──────────┬──┐
│/Users/v64/.bin/j│./knight.j│10│
└─────────────────┴──────────┴──┘
It works if I separate the digits of the parameter into different arguments:
$ ./knight.j 1 0
0 17 96 67 14 19 84 35 12 21
99 64 15 18 97 68 13 20 37 34
16 1 98 95 66 85 36 83 22 11
63 92 65 86 81 94 69 72 33 38
2 87 90 93 76 71 82 39 10 23
91 62 53 78 89 80 75 70 73 32
44 3 88 61 52 77 40 59 24 9
47 50 45 54 79 60 27 74 31 58
4 43 48 51 6 41 56 29 8 25
49 46 5 42 55 28 7 26 57 30
ARGV
┌─────────────────┬──────────┬─┬─┐
│/Users/v64/.bin/j│./knight.j│1│0│
└─────────────────┴──────────┴─┴─┘
I understand conceptually why this works, but I can't figure out how to modify the script to accept "10" as a single argument.
Thanks for the additional information on ARGV.I think the issue is that 0 ". > 2}. ARGV is a list of length 1 when '10' is the third box and an atom with shape empty when '9' is in the third box.
ARGV=: '/Users/v64/.bin/j';'./knight.j';'10'
ARGV
┌─────────────────┬──────────┬──┐
│/Users/v64/.bin/j│./knight.j│10│
└─────────────────┴──────────┴──┘
$ 0 ".>2}. ARGV NB. 1 item list
1
0 ".>2}. ARGV
10
ARGV=: '/Users/v64/.bin/j';'./knight.j';'9'
$ 0 ".>2}. ARGV NB. atom with empty shape
0 ".>2}. ARGV
9
You can change the shape of the '10' result by using {. on the length 1 list to make it an atom and I think you will find that your verb now works for double digits.
ARGV=: '/Users/v64/.bin/j';'./knight.j';'10'
ARGV
┌─────────────────┬──────────┬──┐
│/Users/v64/.bin/j│./knight.j│10│
└─────────────────┴──────────┴──┘
$ {. 0 ".>2}. ARGV NB. Atom with empty shape
{. 0 ".>2}. ARGV
10
I don't imagine this was the reason that you expected, but it does happen from time to time that results that look like atoms are actually 1 item lists which can result in length errors.
Hope this helps.

Gnuplot log plot y-axis

I have have a .txt file with some values as a function of iteration count. And I am trying to log plot it. I have managed to do this with the following code plot 'solchange.txt' using 1:(log($2)) with lines
The x axis is perfect and the shape is perfect, but my y axis is weird. I want it to be say 10^-2 and 10^-3 and so on how can this be done?
What does -16 even mean? My value stops at 10^-7
solchange.txt
1 0.20870164249629861
2 3.0540936828943599E-002
3 2.1622388854567132E-002
4 1.7070994407582529E-002
5 1.4155375579083168E-002
6 1.2069370098131457E-002
7 1.0482626276465484E-002
8 9.2258609277672127E-003
9 8.2010529631910967E-003
10 7.3466561929682317E-003
11 6.6216556909214075E-003
12 5.9973025525987822E-003
13 5.4526144028317000E-003
14 4.9718850942694140E-003
15 4.5432279303643033E-003
16 4.1576291151408026E-003
17 3.8082604242292567E-003
18 3.4899438987894341E-003
19 3.1987266873885617E-003
20 2.9315478643644408E-003
21 2.6859845807917955E-003
22 2.4600648490906499E-003
23 2.2521338021345080E-003
24 2.0607609516045851E-003
25 1.8846776035151558E-003
26 1.7227356558349102E-003
27 1.5738810584753488E-003
28 1.4371370123238449E-003
29 1.3115934299711522E-003
30 1.1964002798033561E-003
31 1.0907632352794312E-003
32 9.9394061400687704E-004
33 9.0524097544450455E-004
34 8.2402100116123200E-004
35 7.4968344624489966E-004
36 6.8167505353953529E-004
37 6.1948438470904935E-004
38 5.6263955830880997E-004
39 5.1070590513836635E-004
40 4.6328356186664012E-004
41 4.2000502958110253E-004
42 3.8053272709547871E-004
43 3.4455657088288370E-004
44 3.1179161480603731E-004
45 2.8197578322736866E-004
46 2.5486773011298286E-004
47 2.3024485386036100E-004
48 2.0790149243582034E-004
49 1.8764731601648640E-004
50 1.6930592515617369E-004
51 1.5271365239171797E-004
52 1.3771855529436799E-004
53 1.2417958039480804E-004
54 1.1196587112561468E-004
55 1.0095618950593192E-004
56 9.1038420860557225E-005
57 8.2109133101636398E-005
58 7.4073166362883126E-005
59 6.6843234255131376E-005
60 6.0339523889261799E-005
61 5.4489287395236962E-005
62 4.9226422450250433E-005
63 4.4491043040285547E-005
64 4.0229044229763214E-005
65 3.6391666180476002E-005
66 3.2935063208632334E-005
67 2.9819883516037614E-005
68 2.7010864597994382E-005
69 2.4476448415957416E-005
70 2.2188419389643915E-005
71 2.0121567231942521E-005
72 1.8253375701611941E-005
73 1.6563737530794070E-005
74 1.5034695117064744E-005
75 1.3650206056065907E-005
76 1.2395932219765905E-005
77 1.1259050839256858E-005
78 1.0228085910393529E-005
79 9.2927581834059692E-006
80 8.4438520049430650E-006
81 7.6730973352498108E-006
82 6.9730653504927742E-006
83 6.3370761471119759E-006
84 5.7591171842940984E-006
85 5.2337712233988323E-006
86 4.7561526453150822E-006
87 4.3218511441380815E-006
88 3.9268819066553586E-006
89 3.5676414890935086E-006
90 3.2408686962536598E-006
91 2.9436098531714777E-006
92 2.6731879338693330E-006
93 2.4271750801384794E-006
94 2.2033681015580162E-006
95 1.9997666008135691E-006
96 1.8145534130165238E-006
97 1.6460770886038423E-006
98 1.4928361827763054E-006
99 1.3534651455843205E-006
100 1.2267216317977253E-006
101 1.1114750729309016E-006
102 1.0066963732594143E-006
103 9.1144860808961906E-007
104 8.2487861805891419E-007
105 7.4620940497668528E-007
106 6.7473324671810456E-007
107 6.0980545750098300E-007
108 5.5083872884395882E-007
109 4.9729799312444450E-007
110 4.4869575892107771E-007
111 4.0458787177585429E-007
112 3.6456965996078949E-007
113 3.2827242845258689E-007
114 2.9536026832557155E-007
115 2.6552715221570336E-007
116 2.3849428917172011E-007
117 2.1400771520012352E-007
118 1.9183609798172768E-007
119 1.7176873612963911E-007
120 1.5361373552607444E-007
121 1.3719634701231387E-007
122 1.2235745044898369E-007
123 1.0895217281928909E-007
log(x) is natural log. You need to use log10(x) if you want base 10.
Another, probably better way would be to use a logarithmic y axis like so:
set format y '%g'
set logscale y
plot 'solchange.txt' using 1:2 with lines
Use help set format to figure out how to change the y-axis tics.

Plotting in R in Linux Terminal

I have a text file that I converted into a numeric vector:
numbers <- scan("list_of_numbers.txt")
I then put it into a table:
t <- table(numbers)
Which outputs like this:
1 2 3 4 5 6 7 8 9 10 11
621266 496647 436229 394595 353249 305882 253983 199455 147380 102872 67255
12 13 14 15 16 17 18 19 20 21 22
41934 24506 13778 7179 3646 1778 816 436 217 114 74
23 24 25 26 27 28 29 30 31 32 33
49 44 26 21 19 21 20 14 9 17 14
34 35 36 37 38 39 40 41 42 43 44
7 11 9 14 3 5 8 4 4 2 3
45 46 47 55 56 60 62 63 69 70 72
2 1 2 2 2 1 1 1 3 2 1
78 82 85 93 95 114 125 265 331 350
1 1 1 1 1 1 1 1 1 1
How would I plot a line graph with x axis of numbers 1 - 25 and y axis the frequency values of the x axis all in the terminal window?
In addition, how can a plot like this (which is default saved as a .pdf file) be viewd in the linux terminal?
Most commands like less, cat, and xdg-open output a bunch of strange unreadable symbols.
You can use fbi, the linux framebuffer imageviewer to open pdf files in the linux console.
A small problem can be that it needs root privileges. It seems like it can not run through R using system, it complains about not being a linux console. But you can use it in the terminal like:
sudo fbi Rplots.pdf
As for the plotting part of your question you can just use something like:
plot(t, xlim = c(1, 25))
Hope it helps,
alex
I think it's very convenient to use txtplot::txtplot as follow:
> cat("1 2 3 4 5 6", file = "list_of_numbers.txt", sep = "\n")
> numbers <- scan("list_of_numbers.txt")
Read 6 items
> t <- table(numbers)
> txtplot(t)
You can install it just by this command:
install.packages('txtplot')
I found that Jupyter may be the best wheel for us to handle that, and we can equip that following this tutorial: Embed Graphs In Jupyter Notebooks in R
References:
How To Install R on Ubuntu 18.04
scan

How to calculate 95th percentile in Excel 2010 [duplicate]

This question already has answers here:
Calculate Percentile in Excel 2010
(3 answers)
Closed 9 years ago.
I am trying to calculate how many calls came back in 95 percentile of time. Below is my Result Set. I am working with Excel 2010
Milliseconds Number
0 1702
1 15036
2 14262
3 13190
4 9137
5 5635
6 3742
7 2628
8 1899
9 1298
10 963
11 727
12 503
13 415
14 311
15 235
16 204
17 140
18 109
19 83
20 72
21 55
22 52
23 35
24 33
25 25
26 15
27 18
28 14
29 15
30 13
31 19
32 23
33 19
34 21
35 20
36 25
37 26
38 13
39 12
40 10
41 17
42 6
43 7
44 8
45 4
46 7
47 9
48 11
49 12
50 9
51 9
52 9
53 8
54 10
55 10
56 11
57 3
58 7
59 7
60 2
61 5
62 7
63 5
64 5
65 2
66 3
67 2
68 1
70 1
71 2
72 1
73 4
74 1
75 1
76 1
77 3
80 1
81 1
85 1
87 2
93 1
96 1
100 1
107 1
112 1
116 1
125 1
190 1
356 1
450 1
492 1
497 1
554 1
957 1
Just some background what does above information means-
1702 calls came back in 0 milliseconds
15036 calls came back in 1 milliseconds
14262 calls came back in 2 milliseconds
etc etc
So to calculate the 95th percentile from the above data, I am using this formula in excel 2010-
=PERCENTILE.EXC(IF(TRANSPOSE(ROW(INDIRECT("1:"&MAX(H$2:H$96))))<=H$2:H$96,A$2:A$96),0.95)
Can anyone help me whether the way I am doing in Excel 2010 is right or not?
I am getting 95th percentile as 10 by using the above scenario.
Thanks for the help.
that's essentially the same question you asked here and the formula I suggested. As per my last comments in that question - that formula should work OK as long as you use CTRL+SHIFT+ENTER correctly. I get 10 as the answer for this example using that formula.
I think you can verify manually that that is indeed the correct answer. If you have a running total in an adjacent column then you can see where the 95th percentile is reached......

Resources