|
1 | 1 | # MOD |
2 | 2 |
|
3 | | -> a MOD b |
| 3 | +> m = a MOD b |
4 | 4 |
|
5 | | -Modulus. Equivalent syntax to the percent character, eg a % b |
| 5 | +Calculates the Modulus. Returns the remainder of `a/b` as integer. Equivalent syntax to % and MDL. |
6 | 6 |
|
| 7 | +### Example 1 |
7 | 8 |
|
8 | | -~~~ |
| 9 | +``` |
| 10 | +print 5 mod 2 ' Output 1 |
| 11 | +print 12 mod 5 ' Output 2 |
| 12 | +``` |
9 | 13 |
|
| 14 | +### Example 2: ASCII table |
| 15 | + |
| 16 | +``` |
10 | 17 | ' LOCATE MOD CHR ASC.bas SmallBASIC 0.12.2 [B+=MGA] 2016-03-23 |
11 | | -' LOCATE row, column - sets the next print location on screen, rows down, columns across |
12 | | -' a MOD b - returns the remainder of a/b as integer 0 to b-1 |
13 | | -' for example odd number n mod 2 returns 1, while even number n mod 2 returns 0 |
14 | | -' n mod 10 returns 0,1,2,3,4,5,6,7,8 or 9 we will use this is demo |
15 | | -' CHR - returns the CHaRracter for the ASC number, for demo we will print a chart of CHR for ASC numbers 32-128 |
16 | | -' ASC(Character) - is a number code for a print characters, 32 is the code for a space |
17 | | -' ? - is shortcut for PRINT |
18 | | -' RIGHT(string,n) - returns right most n characters of string |
19 | | -' STR(n) - returns a number in string form |
20 | | -' : - code statement seperator often used with LOCATE row, column : ? string |
21 | | -' PAUSE optional-number-of-secs - waits for key press or mouse click and/or for a number seconds |
22 | | -' so lets user decide how long to wait |
23 | | -LOCATE 1,16 : ? "ASC Table 30-129:" ' locate print spot, print title for our app |
24 | | -FOR column=0 to 9 'print a header, 10 numbers plus + (to add to row value) |
25 | | - LOCATE 2,column*5+4 : ? "+";column |
| 18 | +' |
| 19 | +' LOCATE row, column : sets the next print location on screen, rows down, columns across |
| 20 | +' a MOD b : returns the remainder of a/b as integer |
| 21 | +' CHR : returns the CHaRracter for the ASC number |
| 22 | +' for demo we will print a chart of CHR for ASC numbers 32-128 |
| 23 | +' ASC(Character) : is a number code for a print characters, 32 is the code for a space |
| 24 | +' ? : is shortcut for PRINT |
| 25 | +' RIGHT(string,n) : returns right most n characters of string |
| 26 | +' STR(n) : returns a number in string form |
| 27 | +' : : code statement seperator often used with LOCATE row, column : ? string |
| 28 | +
|
| 29 | +LOCATE 1,16 : PRINT "ASC Table 30-129:" ' locate print spot, print title for our app |
| 30 | +FOR column = 0 to 9 ' print a header, 10 numbers plus + (to add to row value) |
| 31 | + LOCATE 2, column * 5 + 4 : PRINT "+"; column |
26 | 32 | NEXT |
27 | | -FOR row=3 to 12 |
28 | | - LOCATE row,0 : ? RIGHT(" "+STR(row*10)+":",4) |
| 33 | +FOR row = 3 to 12 |
| 34 | + LOCATE row, 0 : ? RIGHT(" " + STR(row * 10) + ":", 4) |
29 | 35 | NEXT |
30 | 36 | 'main table |
31 | | -FOR ASCnumber=30 to 129 'note ASC(32) = space so wont see anything in Table |
32 | | - row=ASCnumber\\10 ' \\ rounds division down to integer |
33 | | - column=(ASCnumber MOD 10)*5+5 'times 5 to space out the characters printed plus 5 for column labels |
34 | | - LOCATE row,column : ? CHR(ASCnumber) |
| 37 | +FOR ASCnumber = 30 to 129 ' note ASC(32) = space so wont see anything in Table |
| 38 | + row = ASCnumber \ 10 ' \ rounds division down to integer |
| 39 | + column = (ASCnumber MOD 10) * 5 + 5 ' times 5 to space out the characters printed plus 5 for column labels |
| 40 | + LOCATE row, column : ? CHR(ASCnumber) |
35 | 41 | NEXT |
36 | | -PAUSE |
37 | | -
|
38 | | -~~~ |
39 | | - |
| 42 | +``` |
40 | 43 |
|
41 | | -~~~ |
| 44 | +### Example 2: Prime or lowest divisor table |
42 | 45 |
|
| 46 | +``` |
43 | 47 | ' more MOD.bas SmallBASIC 0.12.2 [B+=MGA] 2016-03-23 |
| 48 | +' |
44 | 49 | ' n MOD m - returns the remainder of n divided by m, if 0 then m divides n perfectly |
45 | 50 | ' another way to do MOD in SmallBASIC is to use symbol %, n%m is same as n MOD m |
46 | 51 | ' MOD is great to tell if a number is divisible by another (leaves no remainders) |
47 | 52 | ' a number not divisible by any number less to it other than 1, is called a prime number |
48 | 53 | ' here we will list first 100 numbers and tell if prime or give the lowest divisor |
| 54 | +
|
49 | 55 | 'table setup: title header and row labels |
50 | | -LOCATE 0,0 : ? "P=Prime or Lowest Divisor Table" |
51 | | -FOR column=0 to 9 'print a header, 10 numbers plus + (to add to row value) |
52 | | - LOCATE 2,column*2+7 : ? "+";column |
| 56 | +LOCATE 0,0 : PRINT "P=Prime or Lowest Divisor Table" |
| 57 | +FOR column = 0 to 9 ' print a header, 10 numbers plus + (to add to row value) |
| 58 | + LOCATE 2, column * 2 + 7 : PRINT "+"; column |
53 | 59 | NEXT |
54 | 60 | FOR row=3 to 12 |
55 | | - LOCATE row,0 : ? RIGHT(" "+STR(row*10-30)+":",6) |
| 61 | + LOCATE row, 0 : PRINT RIGHT(" "+STR(row * 10 - 30) + ":", 6) |
56 | 62 | NEXT |
| 63 | +
|
57 | 64 | 'main table data |
58 | | -FOR n=1 to 99 |
59 | | - IF n=1 THEN |
60 | | - report=" O" 'one is one, neither prime nor not prime |
61 | | - ELSE |
62 | | - report=" P" 'letter code for Prime |
63 | | - FOR i=2 TO n-1 |
64 | | - IF n%i=0 THEN '<== if n MOD i=0 or n%i=0, THEN i divides n perfectly |
65 | | - report=" "+STR(i):EXIT 'we found lowest divisor get out of loop |
66 | | - END IF |
67 | | - NEXT |
68 | | - END IF |
69 | | - row=n\\10+3 'n\\10 is our number divided by 10 and rounded down, |
70 | | - 'call it the tens row offset 3 rows down for title and header and blank line |
71 | | - column=n%10*2+7 '<== use MOD to LOCATE the column (*2 column width + 7 row label offset) |
72 | | - LOCATE row,column :? report '? short for print |
| 65 | +FOR n= 1 to 99 |
| 66 | + IF n = 1 THEN |
| 67 | + report = " O" ' one is one, neither prime nor not prime |
| 68 | + ELSE |
| 69 | + report = " P" ' letter code for Prime |
| 70 | + FOR i = 2 TO n - 1 |
| 71 | + IF n % i = 0 THEN ' <== if n MOD i=0 or n%i=0, THEN i divides n perfectly |
| 72 | + report = " " + STR(i) |
| 73 | + EXIT ' we found lowest divisor get out of loop |
| 74 | + END IF |
| 75 | + NEXT |
| 76 | + ENDIF |
| 77 | + row = n \ 10 + 3 ' n\10 is our number divided by 10 and rounded down, |
| 78 | + ' call it the tens row offset 3 rows down for title and header and blank line |
| 79 | + column = n % 10 * 2 + 7 ' <== use MOD to LOCATE the column (*2 column width + 7 row label offset) |
| 80 | + LOCATE row, column : ? report ' ? short for print |
73 | 81 | NEXT |
74 | | -?:?"O=one is neither prime nor not" ' ?:?"..." print blank line first |
75 | | -PAUSE |
76 | | -
|
77 | | -~~~ |
78 | | - |
| 82 | +?:? "O=one is neither prime nor not prime" ' ?:?"..." print blank line first |
| 83 | +``` |
79 | 84 |
|
0 commit comments