# A list of number modulus their indices in the list

A simple one: Take a list of positive integers as input and output the numbers modulus their 1-based index in the list.

If the input integers are {a, b, c, d, e, f, g} then the output should be {a%1, b%2, c%3, d%4, e%5, f%6, g%7} where % is the modulus operator.

Test cases:

10  9  8  7  6  5  4  3  2  1
0  1  2  3  1  5  4  3  2  1

8 18  6 11 14  3 15 10  6 19 12  3  7  5  5 19 12 12 14  5
0  0  0  3  4  3  1  2  6  9  1  3  7  5  5  3 12 12 14  5

1
0

1  1
0  1


# C, 52 bytes

i;f(n,l)int*l;{for(i=0;i++<n;)printf("%d ",*l++%i);}


Try it online!

# Ruby, 25 bytes

->x{x.map{|y|$.+=1;y%$.}}


Try it online!

# Perl 6, 7 bytes

*Z%1..*


Test it

## Expanded:

*      # WhateverCode lambda (this is the parameter)
Z[%]   # zipped using infix modulus operator (&infix:«%»)
1 .. * # Range starting from 1 (doesn't stop)


# Common Lisp, 57 bytes

(defun m(l &aux(i 0))(mapcar(lambda(x)(mod x(incf i)))l))


Try it online!

# Python 3, 46 bytes

lambda x:+[x[i]%i for i in range(1,len(x))]


Simples ;)

• Shouldn't it be range(1,len(x)+1)? But then you're better off doing range(len(x)) and doing lambda x:[x[i]%-~i for i in range(len(x))]. – Erik the Outgolfer Jun 15 '17 at 9:17
• @EriktheOutgolfer Well you could do that if you want an IndexError... – Beta Decay Jun 15 '17 at 9:39
• Does not raise any error for me. – Erik the Outgolfer Jun 15 '17 at 9:43
• Oh, and your solution, including my golf, is also compatible with Python 2. – Erik the Outgolfer Jun 15 '17 at 9:44

# Python, 41 bytes

f=lambda l,i=1:l and[l%i]+f(l[1:],i+1)


Try it online!

A recursive solution. One byte longer than using enumerate. Other attempts:

f=lambda l,i=1:l and[l.pop(0)%i]+f(l,i+1)
f=lambda l,i=0:l[i:]and[l[i]%-~i]+f(l,i+1)
f=lambda l:l and f(l[:-1])+[l[-1]%len(l)]


The last one really wants to use pop but the order of evaluation is wrong.

• Save five bytes by printing with def f(a,m=1):print a[m-1]%m;f(a,m+1), but I see that still does not beat your 35 byte full program solution. – Jonathan Allan Jun 14 '17 at 22:09

# Go, 68 bytes

Go isn't hopelessly defeated (kinda is, but nvm that) because it has that nice range which is the equivalent of Python's enumerate().

func f(l[]int)(r[]int){for i,v:=range l{r=append(r,v%(i+1))};return}


Try it online!

# PHP, 37 bytes

<?foreach($_GET as$v)echo$v%++$k." ";


Try it online!

Output separeted by an underscore

PHP, 35 bytes

<?foreach($_GET as$v)echo$v%++$k,_;


Try it online!

# Perl 6, 18 bytes

{map (*+1)R%*,.kv}


Accepts the list as its sole argument.

# k, 12 bytes

{(1+!#x)!'x}


Try it online.

# Perl6, 22 21 bytes

say $_%++$ for @*ARGS


# Store initial index state
# The "++$" in the golf version increments an anonymous state variable for this. my$index = 0;
# Iterate over the command-line arguments
for @*ARGS -> $number { # Increment the current index$index++;
# Print out the result for this element
say $number %$index;
}


Usage:

perl6 whatever-you-named-it.p6 [space-separated numbers go here]


Note that if we don't need to actually print the output, we can remove the initial say, saving 4 bytes to bring the total down to 17.

# Aceto, 12 bytes

%pLI
ILOM
ri


It's a bit hard to see because there's no line break after the result is printed. Input is taken one number at a time, seperated by linebreaks.

### Explanation:

Read a line and cast it to an integer:

ri


Load quick storage (initially an empty string) and increment it (autocasts to int; --> 1)

IL


Calculate and print modulo, load quick storage again, increment and save it:

%pLI
M


Go back to the start:

  O


### Example in- and output:

10
0
9
1
8
2
7
3
6
1
5
5
4
4
3
3
2
2
1
1


For one byte the output can look nicer:

%pLI
ILnM
riO


# Actually, 6 bytes

;ru@♀%


Try it online!

Explanation:

;ru@♀%
;ru     push [1, len(L)]
@♀%  modulo of pairs from each list ([L%1, L%2, ...])


# Ruby, 40 bytes

->q{q.each_with_index.map{|n,i|n%(i+1)}}


# Swift 4, 18 bytes

zip(a,1...).map(%)


# Clojure, 25 bytes

#(map mod %(rest(range)))


(range) generates an infinite sequence of integers starting from 0, rest drops the first, and mapping with multiple sequences applies the operation to corresponding elements (and stops with the shorter sequence).

# jq, 28 characters

to_entries[]|.value%(.key+1)


Sample run:

bash-4.4\$ jq 'to_entries[]|.value%(.key+1)' <<< '[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]'
0
1
2
3
1
5
4
3
2
1


Try in jq‣play

For +5 characters you can enclose all the code in brackets and ask for compact output to get JSON output. But that not seems to be required.

# NewStack, 3 bytes

ᵢ%ṅ


## The breakdown:

ᵢ    Append user's input to the stack
%ṅ  Mod stack by 1-based index of each element