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


($[1..]).zipWith mod  Try it online! A trick to flip I learned from a golf of Anders Kaseorg. Operation Flashpoint scripting language, 73 bytes f={l=_this;r=[];i=0;while{i<count l}do{r=r+[(l select i)%(i+1)];i=i+1};r}  Call with: numList = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; hint format["%1\n%2", numList, numList call f];  Output: • What... this is a thing? – JAD Jun 15, 2017 at 12:35 • @JarkoDubbeldam Yes. The game allows players to create their own scenarios, and there is an in-game scripting language designed to supplement mission designing. However, since the language is Turing-complete, you can do pretty much whatever you want with it. Jun 15, 2017 at 13:22 Python 2, 35 bytes i=1 for x in input():print x%i;i+=1  Try it online! Counts the index up manually, as per a tip of mine. • If you can exit with an error (I forget if that's the default) you can shave a couple bytes. Jun 14, 2017 at 22:39 Jelly, 2 bytes %J  Try it online! Explanation: %J J List 1 .. len(input). This is results in a list of the indexes. % Modulo.  Basically, the code modulos the original list by the list of indexes. • As soon as I saw this question, I thought "that's %J in Jelly, I wonder if anyone has answered with that answer?". I guess someone else had the same idea :-D – user62131 Jun 14, 2017 at 21:49 • @ais523 You think you were the only one? Think again! Jun 15, 2017 at 9:10 R, 24 18 bytes pryr::f(x%%seq(x))  Evaluates to the function: function (x) x%%seq(x)  Which uses seq_along() to create a vector of the same length as x, starting at 1, and then %% to take the modulo. Default behaviour of seq when presented with a vector is seq(along.with = x) which is the same output as seq_along(x), but 6 bytes shorter. • seq(x) is a handy thing to have around, since I'm always using 1:length(x). Jun 15, 2017 at 14:08 • @Giuseppe Yeah I was kinda surprised as well. – JAD Jun 15, 2017 at 14:13 R, 27 bytes x=scan();cat(x%%1:sum(1|x))  saved 5 bytes thanks to @Jarko saved 4 more thanks to @Giuseppe saved 2 more thanks to @Taylor Scott Saved 2 more thanks to @returnbull • 35 it is - removed unneeded last paren Jun 15, 2017 at 13:47 • you don't need the ' ' (space) at the end of cat; that's the default separator Jun 15, 2017 at 13:51 • you can drop 2 bytes to get 33 by reducing this down to x<-scan();cat(x%%1:length(x)," ") -- oh and a couple formatting tips, 1) you only need 4 spaces to the left of your code for it to be properly indented and marked 2) you can add a <!-- language-all: lang-r --> flag before your code to have it be highlighted (though this changes little in this example) 3) you do not need the brakets around your language's name 4) oh and you don't need to make a comment when you make edits to a post Jun 15, 2017 at 13:57 • (1) You can use = instead of <- to save a byte. (2) The specification says "output" rather than "print", so you can probably drop the cat(), saving 5 bytes. (3) sum(1|x) is one byte shorter than length(x). Jun 16, 2017 at 13:51 MATL, 4, 3 bytes tf\  Try it online! One byte saved thanks to @LuisMendo! APL (Dyalog), 5 bytes ⍳∘≢|⊢  Try it online! ⍳ the indices ∘ of ≢ the length of the argument | that modulus ⊢ the argument • Always amazed that a "mainstream" language can be so economical. The APL way seems naturally to be code golf: e.g. (~T∊T∘.×T)/T←1↓⍳R ⍝ primes up to R or life←{↑1 ω∨.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂ω} ⍝ Game of Life – user15259 Jun 14, 2017 at 22:27 • @YiminRong You can do better: Primes to R: (⊢~∘.×⍨)1↓⍳R and GoL (in Version 16.0): K∊⍨⊢∘⊂⌺3 3 where K is a constant. – Adám Jun 14, 2017 at 22:31 • @YiminRong Try the primes finder here! – Adám Jun 14, 2017 at 22:32 Cubix, 19 bytes ;ww.1I!@s%Ow;)Sow.$


Try it online!

    ; w
w .
1 I ! @ s % O w
; ) S o w . $. . . . .  Watch It Run A fairly straight forward implementation. • 1 push 1 to the stack to start the index • I!@ get the integer input and halt if 0 • s%Ow swap the index up, mod, output result and change lane • ;) remove result and increment index • Sow push 32, output space and change lane (heading down from o) • $O jump the output
• w;w change lange, remove 32 from stack and change lane onto the I input

05AB1E, 2 bytes

ā%

ā  # Push the range(1, len(a) + 1)
% # Mod each element in the input by the same one in this list

• Interesting, I thought it'd be like DgL%, nice. Jun 16, 2017 at 21:07
• @carusocomputing I originally had gL% because I forgot about ā. Jun 16, 2017 at 21:57
• mind going a bit more in-depth on ā for me? I believe I've never used it is it just like for each but in a 1 to n+1 manner like vy<code>}) but implied vy<code>})? Jun 16, 2017 at 21:58
• @carusocomputing it pushes an array with values 1 to the length of the popped array. It's equivalent to gL. TIO Jun 16, 2017 at 22:04
• Does it also dupe the input? Or is implicit input now extended automatically to the closest available input? Jun 16, 2017 at 22:06

Mathematica, 22 bytes

#&@@@Mod~MapIndexed~#&


One more Mathematica approach.

• MapIndexed@Mod is almost good enough :'(
– user61980
Jun 14, 2017 at 22:39

Starry, 75 70 bytes

      +  , + +   *    +  + +      +*   +    *  .               + + .'


Try it online!

Explanation

This is an infinite loop that keeps reading numbers from the input and increasing a counter initiallized at 1. For each pair of input and counter, the modulus is computed and printed.

To end the loop when input has been exhausted, the following trick is used. When no more input is available, trying to read one more number gives a 0. Thus, we divide the read number by itself, and if it is 0 the program ends with an error. Else we discard the result and continue.

      +              Push 1. This is the initial value of the counter
Mark label
,                  Read number from input and push it. Gives 0 if no more input
+                   Duplicate top of the stack
+                   Duplicate top of the stack
*                 Pop two numbers and push their division. Error if divisor is 0
+                Pop (discard) top of the stack
+                  Swap top two numbers
+                   Duplicate top of the stack
+              Push 1
*                    Pop two numbers and push their sum. This increases the counter
+                 Rotate stack down, to move increased counter to bottom
*                Pop two numbers and push their modulus
.                  Pop a number and print it as a number
+     Push 10
+                   Duplicate top of the stack
.                   Pop a number (10) and print it as ASCII character (newline)
'                    If top of the stack is non-zero (it is, namely 10) go to label


Brachylog, 9 bytes

l⟦₁;?↔z%ᵐ


Try it online!

Explanation

l⟦₁          [1, ..., length(Input)]
;?↔z      Zip the Input with that range
%ᵐ    Map mod


R, 22 bytes

pryr::f(x%%1:sum(x|1))


R performs 1:length(x) before doing the modulus.

• Nice find with sum(x|1)!
Jun 15, 2017 at 12:51
• Just found out that using seq() instead of seq_along() does the same thing. So that is a few bytes shorter again.
Jun 15, 2017 at 13:02
• I was going to tell you that, but I didn't have the rep to comment. Glad you figured it out. Jun 15, 2017 at 13:12

Itr, 5 bytes

#äL¹%

online interpreter

Explanation

#     ; read the list from standard input
äL   ; push its length
¹  ; converted to 1-based range
% ; point-wise modulo
; implicitly output list


Itr, 4 bytes

#L¹%

The ä before the L is no-longer necessary in newer versions

Japt-m, 54 3 bytes

uVÄ


Try it

-1 byte thanks to ETHproductions

• I think you can save a byte with ®%°T (actually, you could still use Y there if you wanted) Jun 15, 2017 at 11:00
• Aha. Thanks, @ETHproductions. Jun 15, 2017 at 11:02

Python 2, 42 bytes

lambda l:[v%(i+1) for i,v in enumerate(l)]


Try it online!

• You can remove the space before the for Jun 14, 2017 at 21:19
• 41 bytes: lambda l:[v%-~i for i,v in enumerate(l)]
– ovs
Jun 14, 2017 at 21:24
• Or, lambda l:[v%i for i,v in enumerate(l,1)].
– xnor
Jun 14, 2017 at 21:26

zipWith(flip mod)[1..]


Try it online!

Also: flip(zipWith mod)[1..].

Mathematica, 21 bytes

#~Mod~Range@Length@#&


Try it online!

or 20 bytes (by Martin)

#~Mod~Range@Tr[1^#]&

• Tr[1^#] for Length@#. Jun 14, 2017 at 21:33
• that one doesn't work on mathics, so I'm keeping them both Jun 14, 2017 at 21:40
• You are missing a # as the second last character in your first answer. Jun 15, 2017 at 0:26

Excel VBA, 59 46 Bytes

Golfed

Anonymous VBE Immediate window funtion that takes a space ( ) delimited array string as input from range [A1] and output the numbers modulus their 1-based index in the starting list to the VBE immediate window

For Each n In Split([A1]):i=i+1:?n Mod i;:Next


Input / Output:

[A1]="10 9 8 7 6 5 4 3 2 1" ''# or manually set the value
For Each n In Split([A1]):i=i+1:?n Mod i;:Next
0  1  2  3  1  5  4  3  2  1


Old Subroutine version

Subroutine that takes input as a passed array and outouts to the VBE immediate window.

Sub m(n)
For Each a In n
i=i+1
Debug.?a Mod i;
Next
End Sub


Input / Ouput:

m Array(10,9,8,7,6,5,4,3,2,1)
0  1  2  3  1  5  4  3  2  1


Ungolfed

Option Private Module
Option Compare Binary
Option Explicit
Option Base 0 ''# apparently Option Base 1 does not work with ParamArrays

Public Sub modIndex(ParamArray n() As Variant)
Dim index As Integer
For index = LBound(n) To UBound(n)
Debug.Print n(index) Mod (index + 1);
Next index
End Sub


Input / Output:

Call modIndex(10,9,8,7,6,5,4,3,2,1)
0  1  2  3  1  5  4  3  2  1


Thunno 2, 2 bytes

ż%


Try it online!

Explanation

ż%  # Implicit input
$# Vectorised modulo ż # List [1..length] # Implicit output  Rust, 46 bytes |v|v.iter().zip(1..).map(|(n,i)|n%i).collect()  Rust Playground • Welcome to Code Golf, and nice first answer! Aug 4, 2023 at 5:47 CJam, 9 bytes {_,,:).%}  Anonymous block that expects an array on the stack and replaces it by the output array. Try it online! Explanation { } e# Define block _ e# Duplicate , e# Length , e# Range, 0-based :) e# Add 1 to each entry .% e# Vectorized modulus  J, 9 bytes >:@i.@#|[  1 ... n | original list | is mod JavaScript (ES6), 22 bytes a=>a.map((x,y)=>x%++y)  AWK, 13 {print$1%NR}


tcl, 35

lmap l $L {puts [expr$l%[incr i]]}


GNU APL 1.2, 9 bytes

(⍳⍴R)|R←⎕


APL operates from right to left, hence the parentheses.

R←⎕ assigns user input to vector R.

⍴R gives the length of the vector; ⍳⍴R gives a vector with all numbers from 1 to that length (so the indices).

| is the mod operator (a|b yields b%a). APL operates on arrays, so the code snippet a vector containing each element from the user's input mod its index.

Pari/GP, 22 bytes

a->[a[n]%n|n<-[1..#a]]


Try it online!

Pyth, 5

.e%bh

    hk     # 1-based index of (implicit) lambda variable
b       # element
%        # element mod (1-based index)
.e    Q    # enumerated map over (implicit) input