Back to the Basics of Math

5

Introduction:

We all know these three basic arithmetic math tables I assume.

ADDITION:
 + | 1  2  3  4  5  6  7  8  9  10
----------------------------------
1  | 2  3  4  5  6  7  8  9  10 11
2  | 3  4  5  6  7  8  9  10 11 12
3  | 4  5  6  7  8  9  10 11 12 13
4  | 5  6  7  8  9  10 11 12 13 14
5  | 6  7  8  9  10 11 12 13 14 15
6  | 7  8  9  10 11 12 13 14 15 16
7  | 8  9  10 11 12 13 14 15 16 17
8  | 9  10 11 12 13 14 15 16 17 18
9  | 10 11 12 13 14 15 16 17 18 19
10 | 11 12 13 14 15 16 17 18 19 20

SUBTRACTION:
 - | 1  2  3  4  5  6  7  8  9  10
---------------------------------
1  | 0  1  2  3  4  5  6  7  8  9
2  | -1 0  1  2  3  4  5  6  7  8
3  | -2 -1 0  1  2  3  4  5  6  7
4  | -3 -2 -1 0  1  2  3  4  5  6
5  | -4 -3 -2 -1 0  1  2  3  4  5
6  | -5 -4 -3 -2 -1 0  1  2  3  4
7  | -6 -5 -4 -3 -2 -1 0  1  2  3
8  | -7 -6 -5 -4 -3 -2 -1 0  1  2
9  | -8 -7 -6 -5 -4 -3 -2 -1 0  1
10 | -9 -8 -7 -6 -5 -4 -3 -2 -1 0

MULTIPLICATION:
 * | 1  2  3  4  5  6  7  8  9  10
----------------------------------
1  | 1  2  3  4  5  6  7  8  9  10
2  | 2  4  6  8  10 12 14 16 18 20
3  | 3  6  9  12 15 18 21 24 27 30
4  | 4  8  12 16 20 24 28 32 36 40
5  | 5  10 15 20 25 30 35 40 45 50
6  | 6  12 18 24 30 36 42 48 54 60
7  | 7  14 21 28 35 42 49 56 63 70
8  | 8  16 24 32 40 48 56 64 72 80
9  | 9  18 27 36 45 54 63 72 81 90
10 | 10 20 30 40 50 60 70 80 90 100

For the sake of this challenge, division is excluded.

Sequence definition:

We will follow the same steps for both a 0-indexed and 1-indexed sequence. So the resulting sequence will become f(0)+g(1), f(1)+g(2), etc.

  • Both f (0-indexed) and g (1-indexed) start in the ADDITION table at (1,1). So f(0) = 1+1 = 2 and g(1) = 1+1 = 2.
  • We then go to the SUBTRACTION table, and we either go n steps down if n is odd, or n steps to the right if n is even. So f(1) = 1-2 = -1 (it went to the down, because n=1 is odd); and g(2) = 3-1 = 2 (it went right, because n=2 is even).
  • Then we go to the MULTIPLICATION table, and do the same: f(2) = 3*2 = 6 and g(3) = 3*4 = 12.
  • And then we go back to the ADDITION table again. Also, when we go beyond the borders of the 10x10 table, we wrap around from the bottom to the top, or from the right to the left. So going from (7,9) six steps right, will end up at (3,9).

The resulting sequence will be f(n)+g(n+1) (with n being 0-indexed).

Here the first six in colors, in the order green → blue → purple → grey → red → orange:

f (0-indexed): (1+1) → (1-2) → (3*2) → (3+5) → (7-5) → (7*10)

enter image description here

g (1-indexed): (1+1) → (3-1) → (3*4) → (7+4) → (7-9) → (3*9)

enter image description here

The resulting sequence is the sum of both these sequences.

Here are the first 100 items of the 0-indexed sequence f:

2,-1,6,8,2,70,13,-4,7,7,-5,7,10,-7,70,12,-2,6,3,0,1,3,1,15,12,-3,30,10,-6,6,7,-6,21,13,-3,35,8,1,2,2,0,2,5,-2,35,17,-7,21,8,-5,6,8,-4,30,17,2,15,5,-1,1,2,-1,6,8,2,70,13,-4,7,7,-5,7,10,-7,70,12,-2,6,3,0,1,3,1,15,12,-3,30,10,-6,6,7,-6,21,13,-3,35,8,1,2,2

Here are the first 100 items of the 1-indexed sequence g:

2,2,12,11,-2,27,9,-5,5,6,-5,18,12,-2,28,7,2,1,11,-9,1,4,-1,28,16,-6,18,7,-4,5,7,-3,27,16,3,12,4,0,10,11,0,3,7,3,63,12,-3,6,6,-4,6,9,-6,63,11,-1,3,2,-9,10,2,2,12,11,-2,27,9,-5,5,6,-5,18,12,-2,28,7,2,1,11,-9,1,4,-1,28,16,-6,18,7,-4,5,7,-3,27,16,3,12,4,0,10,11

Here are the first 100 items of the result sequence:

4,1,18,19,0,97,22,-9,12,13,-10,25,22,-9,98,19,0,7,14,-9,2,7,0,43,28,-9,48,17,-10,11,14,-9,48,29,0,47,12,1,12,13,0,5,12,1,98,29,-10,27,14,-9,12,17,-10,93,28,1,18,7,-10,11,4,1,18,19,0,97,22,-9,12,13,-10,25,22,-9,98,19,0,7,14,-9,2,7,0,43,28,-9,48,17,-10,11,14,-9,48,29,0,47,12,1,12,13

Challenge:

Given an integer n, output the n'th number of the above result sequence (which sums both the 0-indexed and 1-indexed sequences). So calculate h(n) = f(n)+g(n+1).

Challenge rules:

  • The result-sequence is always 0-indexed. I know some languages are 1-indexed by default, in which case you'll have to do n-1 right after entering the function/program so f(n)+g(n+1) will start at f(0)+g(1) (and not f(1)+g(2)).
  • Output can be in any reasonable format, so a String or decimal with .0 instead of an integer is allowed.
  • As you may or may not have noted; in the tables above the number at the top is taken first, then the number at the left.

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.

Test cases (0-indexed):

0       4
1       1
2       18
3       19
4       0
5       97
10      -10
50      12
60      4
61      1
62      18
63      19
100     0
111     17
1000    0
1994    98
2497    1
2498    12
2499    13
2500    0

Note/tip: The sequences are self-repeating every 60 items.

Kevin Cruijssen

Posted 2017-10-03T06:54:28.600

Reputation: 67 575

2Darn. – totallyhuman – 2017-10-03T07:01:05.093

@icrieverytim Hehe, sorry about that. Came up with the sequence myself. Then again, I have come up with sequences in the past which did have an existing OEIS page. ;) – Kevin Cruijssen – 2017-10-03T07:03:27.353

This one surprised me. I didn't even search for it on OEIS since I came up with it myself and thought it was an original idea. – Stewie Griffin – 2017-10-03T07:20:28.100

Probably missing something obvious, but in the subtraction step of your sequence definition, I don't see how g(2) got to position (5, 7) by going 2 steps to the left... – Jonah – 2017-10-03T13:35:53.257

@Jonah g(2) = 3-1 = 2. I'm not sure where you're seeing the (5,7) tbh.. Unless I'm reading past it. Could you give a quote of where you see the g(2) with (5,7)? – Kevin Cruijssen – 2017-10-03T13:41:16.280

2Maybe I'm misinterpreting something here, but it seems like you're going to the right when it is even, not to the left. E.g. f(1) is at (1,2), and f(2) is 2 steps to the right at (3,2). – RothX – 2017-10-03T13:44:03.417

Originally I was looking at the f picture when I should have been looking at the g picture, hence the (5, 7) confusion. But now I'm still confused per RothX's comment. The blue square in the g picture has moved right 2 spaces... – Jonah – 2017-10-03T13:49:08.557

Answers

5

05AB1E, 48 47 43 42 bytes

1-indexed.

¾4×SsFD2äεT%>`…+-*Nè.V}Os0NÌ‚ÂTS-‚Nƒí}˜+}\

Try it online! or see the Whole sequence

Explanation

¾4×S                                         # initialize stack with [0,0,0,0]
    sF                                  }    # for N in range [0 ... input-1] do:
      D                                      # duplicate
       2äε            }                      # for each pair of values
          T%>                                # get last digit of each and increment
              …+-*Nè                         # index into "+-*" with N
             `      .V                       # apply the function to our values
                       O                     # sum the pair
                        s                    # swap the list of 4 values to the top
                         0NÌ‚                # push [0, N+2]
                             ÂTS-            # bifurcate and subtract [1,0]
                                 ‚           # pair, resulting in [[0,N+2],[N+1,0]]
                                  NĒ}       # reverse each pair N+1 times
                                      ˜+     # flatten and add to the list of values
                                               from last iteration
                                         \   # discard the final list

Emigna

Posted 2017-10-03T06:54:28.600

Reputation: 50 798

¾4×S can be 4Å0 or ¾4и now for -1 byte. – Kevin Cruijssen – 2019-03-29T14:18:03.003

6

APL (Dyalog), 77 bytes

Full program which prompts for n, looks up the result in the list of 60 possible results, and prints that to STDOUT.

¯50+⎕AV⍳'63DE2èHy£¢xKHyéE29∆y492ýNyÂCx$∆yÂO2Á£3£¢27£3éOxM∆y£CxäN3D9x$'[60|⎕]

Try it online!

''[] index the string by the following:

 prompt for n

60| remainder when dividing by 60

⎕AV⍳ find ɩndices in the Atomic Vector (the character set)

¯50+ add negative fifty

Adám

Posted 2017-10-03T06:54:28.600

Reputation: 37 779

5

Python 3, 89 bytes

lambda i:ord('HEVWD¥Z;PQ:]Z;¦WDKR;FKDo`;tU:OR;taDsPEPQDIPE¦a:_R;PU:¡`EVK:O'[i%60])-68

Try it online!

ovs

Posted 2017-10-03T06:54:28.600

Reputation: 21 408

5

JavaScript (ES6), 95 ... 84 82 bytes

This is the best I could come up with so far without hardcoding the sequence.

Edit: Now probably shorter than hardcoding anyway.

m=>(g=(x,y=n=1)=>n>m?[x*y,x+y,n+o--&1?y-x:x-y][n%3]:g((y-o+n++)%10+1,x))(o=1)+g(1)

Demo

let f =

m=>(g=(x,y=n=1)=>n>m?[x*y,x+y,n+o--&1?y-x:x-y][n%3]:g((y-o+n++)%10+1,x))(o=1)+g(1)

;[0, 1, 2, 3, 4, 5, 10, 50, 60, 61, 62, 63, 100, 111, 1000]
.forEach(i => {
  console.log(`a(${i}) = ${f(i)}`)
});

Formatted and commented

m => (                              // m = input
  g = (                             // g = recursive function using:
    x, y = n = 1                    //   (x,y) = coordinates, n = counter
  ) =>                              //   and o = offset (0 or 1)
    n > m ?                         // if n is greater than the input:
      [                             //   evaluate the current cell in the current table
        x * y,                      //   both multiplication
        x + y,                      //   and addition are commutative
        n + o-- & 1 ? y - x : x - y //   but subtraction is not: we need to make sure that
      ][n % 3]                      //   x and y are set in the correct order
    :                               // else:
      g(                            //   do a recursive call to g():
        (y - o + n++) % 10 + 1,     //     updating y and using it as x; incrementing n
        x                           //     using x as y
      )                             //   end of recursive call
  )(o = 1) + g(1)                   // invoke g() with o = 1; then o = 0; return the sum

Arnauld

Posted 2017-10-03T06:54:28.600

Reputation: 111 334

3

Excel, 188 bytes

Hardcoded results:

=CHOOSE(MOD(A1,60)+1,5,2,19,20,1,98,23,-8,13,14,-9,26,23,-8,99,20,1,8,15,-8,3,8,1,44,29,-8,49,18,-9,12,15,-8,49,30,1,48,13,2,13,14,1,6,13,2,99,30,-9,28,15,-8,13,18,-9,94,29,2,19,8,-9,12)-1

Saved 3 bytes by adding 1 to all results in lookup, then subtracting at end. This turns -10 into -9 5 times.

Wernisch

Posted 2017-10-03T06:54:28.600

Reputation: 2 534

2

PowerShell, 81 bytes (UTF-8)

'HEVWD¥Z;PQ:]Z;¦WDKR;FKDo`;tU:OR;taDsPEPQDIPE¦a:_R;PU:¡`EVK:O'["$args"%60]-68

Try it online!

Port of ovs' answer to PowerShell. Since after reading the challenge, I was going to do something similar, it's easier to just re-use the string.

AdmBorkBork

Posted 2017-10-03T06:54:28.600

Reputation: 41 581

2

Haskell, 159 155 bytes

EDIT: thanks to Laikoni for taking off 4 bytes!

((z(+)(a 1)$a 0)!!)
p#n=mod(p+n-1)10+1
b i=(1,1):[(p#(n*0^e),q#(n*e))|n<-[1+i..],let(p,q)=b i!!(n-1-i);e=mod n 2]
a=z uncurry o.b
o=(+):(-):(*):o
z=zipWith

Usage:

(zipWith(+)(a 1)$a 0)!!5

Try it online!

Explanation:

The # operator is for incrementing a coordinate by some value n and wrapping it. Notice the -1 and +1 to get it onto the range [0..9] so mod will work right.

The b function builds an infinite list of coordinates given an offset value of 0 or 1 to simulate the difference between 0 indexing and 1 indexing. b works by recursively iterating on the coordinates, starting with a hardcoded value of (1,1). When updating a coordinate we need to make sure we don't change it if n doesn't have the right parity so I multiply n by some value that will be 0 or 1 depending on its parity. e will be 0 when n is even and 1 otherwise and the 0^ trick can invert that for the other case.

The a function also takes an offset, passes it to b and zips the result with an infinite list cycling between the addition, subtraction and multiplication operators. The zip is performed with the uncurry function which breaks up the coordinate pair and applies them to the operator.

Finally we build the resulting sequence by simply zipping the lists (generated with different offsets) with the addition operator. Then we use the (!!) operator to turn it into a lookup function.

user1472751

Posted 2017-10-03T06:54:28.600

Reputation: 1 511

1

Nice answer, +1 from me. Could you perhaps add a Try It Online link? Haskell should also be among the available TIO programming languages.

– Kevin Cruijssen – 2017-10-04T06:48:57.920

1

Defining z=zipWith and o=(+):(-):(*):o saves some bytes: Try it online! (I also added a command line flag to be able to define s in the header section.)

– Laikoni – 2017-10-05T21:41:53.583

Also your current byte count seems of, it should be 159 bytes. – Laikoni – 2017-10-05T21:42:59.693

1

Python 2, 85 bytes

lambda j:ord('!/0~3)*630$+$H9M.(+M:L))*"):8+).z9/$('[j%60])-29

Try it online!

Based off of ovs' Python 3 answer; implemented in Python 2 without using non-ASCII characters.

Jonathan Frech

Posted 2017-10-03T06:54:28.600

Reputation: 6 681

0

Java 8, 191 85 bytes

n->"HEVWD¥Z;PQ:]Z;¦WDKR;FKDo`;tU:OR;taDsPEPQDIPE¦a:_R;PU:¡`EVK:O".charAt(n%60)-68

-106 bytes by porting @ovs' Python 3 answer.

I lost my initial solution that I've used to create the test cases.. :( But the hard-coded result is way shorter anyway.
I didn't knew the sequence was repeating every 60 items until after I posted the challenge. ;)

Try it online.

Kevin Cruijssen

Posted 2017-10-03T06:54:28.600

Reputation: 67 575