⍝ Natural number arithmetic:

    999 +nats 1
1000
    1000 -nats 1
999
    999 ×nats 999
998001
    (1234 ×nats 5678) ÷nats 1234
5678
      {↑÷nats⍨/(⍵⍴⊂⎕d),⊂⎕d*nats ⍵+1}10
123456789

    3×nats '33333333333'
99999999999

    ↑×nats/⍳100     ⍝ !100
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

    0+nats 0
0
    1-nats 1
0
    0×nats 0
0
    123 ×nats 0
0
    table←{⍵∘.((⍎⍺)nats)⍵}

    ⍕¨'+×*'table¨⊂0,⍳5
┌───────────────────┬──────────────────────┬──────────────────────────┐
│ 0  1  2  3  4  5  │ 0  0  0   0   0   0  │ 1  0  0   0    0    0    │
│ 1  2  3  4  5  6  │ 0  1  2   3   4   5  │ 1  1  1   1    1    1    │
│ 2  3  4  5  6  7  │ 0  2  4   6   8   10 │ 1  2  4   8    16   32   │
│ 3  4  5  6  7  8  │ 0  3  6   9   12  15 │ 1  3  9   27   81   243  │
│ 4  5  6  7  8  9  │ 0  4  8   12  16  20 │ 1  4  16  64   256  1024 │
│ 5  6  7  8  9  10 │ 0  5  10  15  20  25 │ 1  5  25  125  625  3125 │
└───────────────────┴──────────────────────┴──────────────────────────┘
    ⍕¨'<≤=≥>≠'table¨⊂0 1 2               ⍝ relational fns.
┌─────────┬─────────┬─────────┬─────────┬─────────┬─────────┐
│ 0  1  1 │ 1  1  1 │ 1  0  0 │ 1  0  0 │ 0  0  0 │ 0  1  1 │
│ 0  0  1 │ 0  1  1 │ 0  1  0 │ 1  1  0 │ 1  0  0 │ 1  0  1 │
│ 0  0  0 │ 0  0  1 │ 0  0  1 │ 1  1  1 │ 1  1  0 │ 1  1  0 │
└─────────┴─────────┴─────────┴─────────┴─────────┴─────────┘
    ⍕¨'⌈⌊'table¨⊂0,⍳9
┌──────────────────────────────┬──────────────────────────────┐
│ 0  1  2  3  4  5  6  7  8  9 │ 0  0  0  0  0  0  0  0  0  0 │
│ 1  1  2  3  4  5  6  7  8  9 │ 0  1  1  1  1  1  1  1  1  1 │
│ 2  2  2  3  4  5  6  7  8  9 │ 0  1  2  2  2  2  2  2  2  2 │
│ 3  3  3  3  4  5  6  7  8  9 │ 0  1  2  3  3  3  3  3  3  3 │
│ 4  4  4  4  4  5  6  7  8  9 │ 0  1  2  3  4  4  4  4  4  4 │
│ 5  5  5  5  5  5  6  7  8  9 │ 0  1  2  3  4  5  5  5  5  5 │
│ 6  6  6  6  6  6  6  7  8  9 │ 0  1  2  3  4  5  6  6  6  6 │
│ 7  7  7  7  7  7  7  7  8  9 │ 0  1  2  3  4  5  6  7  7  7 │
│ 8  8  8  8  8  8  8  8  8  9 │ 0  1  2  3  4  5  6  7  8  8 │
│ 9  9  9  9  9  9  9  9  9  9 │ 0  1  2  3  4  5  6  7  8  9 │
└──────────────────────────────┴──────────────────────────────┘
    ⍕¨'÷|'table¨⊂⍳9
┌───────────────────────────┬───────────────────────────┐
│ 1  0  0  0  0  0  0  0  0 │ 0  0  0  0  0  0  0  0  0 │
│ 2  1  0  0  0  0  0  0  0 │ 1  0  1  0  1  0  1  0  1 │
│ 3  1  1  0  0  0  0  0  0 │ 1  2  0  1  2  0  1  2  0 │
│ 4  2  1  1  0  0  0  0  0 │ 1  2  3  0  1  2  3  0  1 │
│ 5  2  1  1  1  0  0  0  0 │ 1  2  3  4  0  1  2  3  4 │
│ 6  3  2  1  1  1  0  0  0 │ 1  2  3  4  5  0  1  2  3 │
│ 7  3  2  1  1  1  1  0  0 │ 1  2  3  4  5  6  0  1  2 │
│ 8  4  2  2  1  1  1  1  0 │ 1  2  3  4  5  6  7  0  1 │
│ 9  4  3  2  1  1  1  1  1 │ 1  2  3  4  5  6  7  8  0 │
└───────────────────────────┴───────────────────────────┘
    ⍕¨'∨∧'table¨⊂0,⍳9
┌──────────────────────────────┬──────────────────────────────────────┐
│ 0  1  2  3  4  5  6  7  8  9 │ 0  0  0   0   0   0   0   0   0   0  │
│ 1  1  1  1  1  1  1  1  1  1 │ 0  1  2   3   4   5   6   7   8   9  │
│ 2  1  2  1  2  1  2  1  2  1 │ 0  2  2   6   4   10  6   14  8   18 │
│ 3  1  1  3  1  1  3  1  1  3 │ 0  3  6   3   12  15  6   21  24  9  │
│ 4  1  2  1  4  1  2  1  4  1 │ 0  4  4   12  4   20  12  28  8   36 │
│ 5  1  1  1  1  5  1  1  1  1 │ 0  5  10  15  20  5   30  35  40  45 │
│ 6  1  2  3  2  1  6  1  2  3 │ 0  6  6   6   12  30  6   42  24  18 │
│ 7  1  1  1  1  1  1  7  1  1 │ 0  7  14  21  28  35  42  7   56  63 │
│ 8  1  2  1  4  1  2  1  8  1 │ 0  8  8   24  8   40  24  56  8   72 │
│ 9  1  1  3  1  1  3  1  1  9 │ 0  9  18  9   36  45  18  63  72  9  │
└──────────────────────────────┴──────────────────────────────────────┘

    99999999999÷nats 3              ⍝ raw argument too big.
11::Bad number: 1E11

    2⍟nats 3                        ⍝ unexpected operand function.
11::Can't do: ⍟

    2-nats 3                        ⍝ subtract larger from smaller.
0
    1÷nats 0                        ⍝ 1÷0 → error
11::Div by 0

    0÷nats 0                        ⍝ 0÷0 → 1
1
    0÷nats 1                        ⍝ 0÷1 → 0
0
    ↑÷nats/⎕d ⎕d *nats¨ 101 100     ⍝ accuracy test.
123456789

    hint←{(⍺↑⍵),'···',(-⍺)↑⍵}       ⍝ first and last ⍺ digits. ···

    10 hint (↑*nats/5/2)-nats 3     ⍝ Ackermann(4, 2)
2003529930···5719156733

    0 1{⍵=0:⊃⍺ ⋄ (1↓⍺,+nats/⍺)∇ ⍵-1}1000      ⍝ 1000th Fibonacci number (JRC).
43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875

⍝ Handy prefixes for the first few 2*10×⍵:
⍝
    prefs ← 'kilo' 'mega' 'giga' 'tera' 'peta' 'exa' 'zetta' 'yotta' '...'

    ⍕⍉↑prefs{⍺ ⍵}2*nats¨ 10 20 to 200
 kilo   1024                                                          
 mega   1048576                                                       
 giga   1073741824                                                    
 tera   1099511627776                                                 
 peta   1125899906842624                                              
 exa    1152921504606846976                                           
 zetta  1180591620717411303424                                        
 yotta  1208925819614629174706176                                     
 ...    1237940039285380274899124224                                  
        1267650600228229401496703205376                               
        1298074214633706907132624082305024                            
        1329227995784915872903807060280344576                         
        1361129467683753853853498429727072845824                      
        1393796574908163946345982392040522594123776                   
        1427247692705959881058285969449495136382746624                
        1461501637330902918203684832716283019655932542976             
        1496577676626844588240573268701473812127674924007424          
        1532495540865888858358347027150309183618739122183602176       
        1569275433846670190958947355801916604025588861116008628224    
        1606938044258990275541962092341162602522202993782792835301376 

⍝ regression tests:

    0|nats 3        ⍝ James Moore's case.
3

    try←{                       ⍝ test 999...s squared.
        exp←(⍵ 2 ⍵ 2-1)/'9801'  ⍝ expected result
        act←×nats⍨⍵/'9'         ⍝ actual result
        act≡exp                 ⍝ actual matches expected.
    }

    try 10          ⍝ 9999999999*2 → 99999999980000000001
1
    ∧/try¨ 100 200 to 1000
1

⍝       try 6e4     ⍝ Roger Hui's case (takes about a minute)
⍝   1

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝
⍝ The following line edits test's copy of nats: ⍝
⍝   s/2*53/2*10/    ⍝ split at 2*10             ⍝
⍝   s/px←6/px←1/    ⍝ use a radix of 10*1       ⍝
⍝ to exercise inner function 'mul's split case. ⍝
⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝

    ⎕fx '2*53' '2*10' subs 'px←6' 'px←1' subs ⎕cr'nats'

    ×nats⍨16/'9'    ⍝ check nats with mul-splitting.
99999999999999980000000000000001

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝
⍝ Some other interesting squares: ⍝
⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝

    pyramid ← {(⌊¯0.5×+/⍵=' ')⌽⍵}       ⍝ rotation of rows to centre number

    sq ← ×nats⍨                         ⍝ ⍵*2

    sqs ← {sq ⍵/⍺}                      ⍝ ⍺⍺..⍺ * 2
                                        ⍝ └─⍵─┘

    pyramid ↑'9'sqs¨ 1 to 10            ⍝ 99..9 * 2     (as above)
         81         
        9801        
       998001       
      99980001      
     9999800001     
    999998000001    
   99999980000001   
  9999999800000001  
 999999998000000001 
99999999980000000001

    pyramid ↑'3'sqs¨ 1 to 10            ⍝ 33..3 * 2
          9         
        1089        
       110889       
      11108889      
     1111088889     
    111110888889    
   11111108888889   
  1111111088888889  
 111111110888888889 
11111111108888888889

    pyramid ↑'6'sqs¨ 1 to 10            ⍝ 66..6 * 2
         36         
        4356        
       443556       
      44435556      
     4444355556     
    444443555556    
   44444435555556   
  4444444355555556  
 444444443555555556 
44444444435555555556

    pyramid ↑'1'sqs¨ 1 to 40            ⍝ 11..1 * 2
                                       1                                       
                                      121                                      
                                     12321                                     
                                    1234321                                    
                                   123454321                                   
                                  12345654321                                  
                                 1234567654321                                 
                                123456787654321                                
                               12345678987654321                               
                              1234567900987654321                              
                             123456790120987654321                             
                            12345679012320987654321                            
                           1234567901234320987654321                           
                          123456790123454320987654321                          
                         12345679012345654320987654321                         
                        1234567901234567654320987654321                        
                       123456790123456787654320987654321                       
                      12345679012345678987654320987654321                      
                     1234567901234567900987654320987654321                     
                    123456790123456790120987654320987654321                    
                   12345679012345679012320987654320987654321                   
                  1234567901234567901234320987654320987654321                  
                 123456790123456790123454320987654320987654321                 
                12345679012345679012345654320987654320987654321                
               1234567901234567901234567654320987654320987654321               
              123456790123456790123456787654320987654320987654321              
             12345679012345679012345678987654320987654320987654321             
            1234567901234567901234567900987654320987654320987654321            
           123456790123456790123456790120987654320987654320987654321           
          12345679012345679012345679012320987654320987654320987654321          
         1234567901234567901234567901234320987654320987654320987654321         
        123456790123456790123456790123454320987654320987654320987654321        
       12345679012345679012345679012345654320987654320987654320987654321       
      1234567901234567901234567901234567654320987654320987654320987654321      
     123456790123456790123456790123456787654320987654320987654320987654321     
    12345679012345679012345679012345678987654320987654320987654320987654321    
   1234567901234567901234567901234567900987654320987654320987654320987654321   
  123456790123456790123456790123456790120987654320987654320987654320987654321  
 12345679012345679012345679012345679012320987654320987654320987654320987654321 
1234567901234567901234567901234567901234320987654320987654320987654320987654321

⍝ test Gianluigi's input formatting function:

    pfmt←{⎕IO ⎕PP←0 16                  ⍝ plain format of a numeric scalar.
        rep←⍕⍵
        ⍵≡rep:⍵
        ~'E'∊rep:rep
        exp←⍎(1+rep⍳'E')↓rep
        0>exp:'_ '~⍨(⎕PP-exp)⍕⍵
        sig←'¯'=⊃rep
        man←⎕PP↑'.'~⍨(sig↓(rep⍳'E')↑rep),⎕PP⍴'0'  ⍝ mantissa
        (sig⍴'¯'),(1+exp)↑man,exp⍴'0'
    }

    456+nats pfmt 123E20
12300000000000000000456

Back to: code

Back to: Workspaces