| Bytes | Lang | Time | Link |
|---|---|---|---|
| 049 | Tcl | 250523T172350Z | sergiol |
| 033 | AWK | 250521T153900Z | xrs |
| 036 | Rust | 230803T233321Z | Jeremy M |
| 011 | Itr | 230803T191230Z | bsoelch |
| 006 | Thunno 2 | 230705T190458Z | The Thon |
| nan | 230226T104218Z | The Thon | |
| 006 | Vyxal | 230226T065306Z | lyxal |
| 010 | Pyt | 230225T221911Z | Kip the |
| 012 | CJam | 230221T203629Z | emirps |
| 041 | Erlang | 230221T214254Z | Yuri Gin |
| 039 | PowerShell Core | 230221T211331Z | Julian |
| 079 | Snap! | 230220T140740Z | qarz |
| 006 | MathGolf | 190930T072449Z | Kevin Cr |
| 023 | Arturo | 230220T110043Z | chunes |
| 079 | Go | 191001T175254Z | S.S. Ann |
| 040 | Clojure | 191107T133632Z | Thoma |
| 013 | Pyth | 191107T011345Z | frank |
| 061 | Forth gforth | 191017T065132Z | Bubbler |
| 008 | Canvas | 191014T053415Z | hakr14 |
| 014 | K oK | 190929T184403Z | Galen Iv |
| 014 | k4 | 191004T140443Z | scrawl |
| 228 | Scratch 3.0 | 191003T051740Z | lyxal |
| 007 | Stax | 191001T184921Z | Khuldrae |
| 075 | Bash | 190929T144133Z | Jonah |
| 021 | Julia 1.0 | 191001T150951Z | Simeon S |
| 041 | TSQL 2008 | 191001T120325Z | t-clause |
| 040 | AWK | 190929T160959Z | Jonah |
| 043 | Racket | 190929T231758Z | MLavrent |
| 034 | PowerShell | 190930T131429Z | Veskah |
| 030 | Zsh | 190929T190852Z | GammaFun |
| 060 | Swift | 190930T122040Z | AZTECCO |
| 111 | Batch | 190930T112723Z | Neil |
| 047 | Java JDK | 190930T092329Z | Olivier |
| 046 | Factor | 190930T080232Z | Galen Iv |
| 053 | Icon | 190930T074252Z | Galen Iv |
| 021 | Octave | 190930T063207Z | tsh |
| 029 | Red | 190929T070845Z | Galen Iv |
| 035 | PHP 7.4 | 190929T034919Z | Night2 |
| 064 | C gcc | 190930T002353Z | ErikF |
| 021 | Perl 6 | 190930T000355Z | Jo King |
| 021 | Ruby | 190929T145359Z | Jonah |
| 028 | R | 190929T113954Z | niko |
| 009 | MATL | 190929T120652Z | flawr |
| 030 | JavaScript ES6 | 190929T001128Z | Arnauld |
| 015 | Wolfram Language Mathematica | 190929T083630Z | att |
| 102 | Retina 0.8.2 | 190929T081132Z | Neil |
| 010 | J | 190929T071639Z | Galen Iv |
| 1212 | TIBasic | 190929T052537Z | pizzapan |
| 018 | Haskell | 190929T012533Z | Jonathan |
| 026 | Python 3 | 190929T005552Z | Jonathan |
| 022 | C# Visual C# Interactive Compiler | 190929T005020Z | Gymhgy |
| 006 | 05AB1E | 190929T004424Z | Jonathan |
| 008 | Japt | 190929T003923Z | Gymhgy |
| 007 | Jelly | 190929T001931Z | Jonathan |
| 009 | APL Dyalog Unicode | 190929T002830Z | Adá |
| 019 | Perl 5 ap MListUtil=Sum | 190929T002137Z | Xcali |
Rust, 36 bytes
|v,i|v[i]*100./v.iter().sum::<f32>()
0-indexed, assuming integers cast to floats before the function call
Itr, 11 bytes
#ä#@áS/100*
zero-indexed, outputs a fraction
7 bytes to output the proportion as a fraction:
#ä#@áS/
Explanation
# ; read the first input
ä ; duplicate it
# ; read the second input
@ ; get the element at that index in the first input
á ; swap the two elements
S ; sum up the array
/ ; divide the element by the sum
100* ; convert the result to percent
; implicit output
Itr, 14 bytes
#ä#@áS/0e100**
outputs the result as a float
Thunno 2, 6 bytes
i$S/ɦ×
0-indexed. Change i to İ for 1-indexing.
Explanation
i$S/ɦ× # Implicit input
i # Index in
$ # Push the first input again
S # Take the sum
/ # Divide
ɦ× # Multiply by 100
# Implicit output
Thunno, \$ 9 \log_{256}(96) \approx \$ 7.41 bytes
AHz1S/Z2*
Uses 1-indexing. Change AH to AI for 0-indexing.
Explanation
AHz1S/Z2* # Implicit input
AH # Index into the list
z1S # Sum the list
/ # Divide
Z2* # Multiply by 10**2
Vyxal, 6 bytes
₌i∑/₁*
Another 6 byter joins the fray. Takes index then list. 0-indexed
Explained
₌i∑/₁*
₌i∑ # Push list[index], sum(list)
/ # ^ / ^
₁* # times 100
Pyt, 10 bytes
Đ←⦋⇹Ʃᵮ/2ᴇ*
Zero-based indexing. Takes the array, then the index
Đ implicit input; Đuplicate
← get input
⦋ get element at index
⇹ swap top two items on stack
Ʃ get Ʃum
ᵮ cast to ᵮloat
/ divide
2ᴇ* multiply by 100; implicit print
CJam, 14 12 bytes
-2 bytes thanks to @luis-mendo
_@=\:+d/1e2*
0-indexed. Takes input as index [list]
Explanation
code | explanation
-------------|------------
_@=\:+d/1e2* | whole program
@= | get the item at the index
_ \:+ | sum the array
d | convert to a *d*ouble
/ | divide
1e2* | multiply by 100
Erlang 41 bytes
f(X,N)->lists:nth(N,X)/lists:sum(X)*100.
PowerShell Core, 39 bytes
param($a,$i)$a[$i]/($a-join'+'|iex)*1e2
Uses 0 indexed input and doesn't do rounding
Snap!, 79 bytes (scratchblocks syntax)
((L)(i))::hat
report((combine(L)using((()+()@addInput)@addInput::grey ring))/(i
This is a "custom block" (function), and it takes a "list" (array) as the first argument and the idnex as the second argument.
Wow, having to manually mark hats/grey rings/grey ring inputs really makes short Snap! code in scratchblocks look relatively ugly.
MathGolf, 7 6 bytes
§\Σ/♀*
0-based indexing.
Explanation:
§ # Index the (implicit) second input-integer into the first (implicit) input-list
\ # Swap so the first (implicit) input-list is at the top of the stack
Σ # Take the sum of that input-list
/ # Divide the earlier indexed number by this sum
♀* # Multiply it by 100
# (after which the entire stack joined together is output implicitly as result)
Go, 79 bytes
func f(a[]float64,i int)float64{s:=0.
for _,v:=range a{s+=v}
return a[i]/s*100}
This has a nice amount of accuracy. It takes a Go slice and uses range to calculate the sum.
Clojure, 40 bytes
(fn[a b](* 1e2(/(nth a b)(apply + a))))
It's nice to know about the 1e2 trick. :-)
Pyth, 13 bytes
c*100@hQeQshQ
First time using Pyth so theres probably some pretty big optimizations here, but I dont know where they are...
0-index, takes input as list, index
Forth (gforth), 61 bytes
: f 8 * over + f@ 0e swap 0 do dup f@ f+ 8 + loop f/ 1e2 f* ;
It's way too painful to work with arrays in Forth...
A function that takes three items (array length, array start address, index; rightmost being the top) from the main stack, and gives the answer on the FP stack. The array contains floating-point values.
How it works
: f ( len addr idx -- F: ans )
8 * over + ( len addr addr[idx] ) \ compute the address for the item
f@ 0e ( len addr F:x F:sum ) \ fetch the float at addr[idx] and push 0 on FP stack
swap 0 do ( addr F:x F:sum ) \ repeat len times..
dup f@ f+ \ fetch the float at addr, add it to the sum
8 + \ increment the addr by one cell
loop \ end loop
f/ 1e2 f* \ compute x / sum * 100
;
Canvas, 8 bytes
@;∑÷AA××
Uses 1-indexing.
The last half just multiplies by 100, so 4 bytes can be saved if percentage over interval [0,1] is allowed as opposed to percentage over interval [0,100].
Explanation of example run: list = [1, 2, 3, 4, 5], index = 5.
Stack Visualization | Executed Instruction | Explanation
------------------------------------+----------------------+-----------------------------------------------------------------------------
..., [1,2,3,4,5], 5, [1,2,3,4,5] | (program start) | Canvas starts with the stack populated with the inputs repeating infinitely
..., [1,2,3,4,5], 5 | @, get item | @NA gets the Nth item of A
..., 5, [1,2,3,4,5] | ;, swap two ToS | Swaps the position of the two top items in the stack
..., 5, 15 | ∑, sum | Sums all items in an array
..., 0.33333333333333333333 | ÷, divide | Divides the two top items in the stack
..., 0.33333333333333333333, 10 | A, push 10 | Pushes 10 to the stack
..., 0.33333333333333333333, 10, 10 | A, push 10 | " "
..., 0.33333333333333333333, 100 | ×, multiply | Multiplies the two top items in the stack
..., 33.333333333333333333 | ×, multiply | " "
... | (end of program) | Pop and print ToS
k4, 14 bytes
{%+/x%100*x y}
can save 1 byte from @Galen Ivanov's solution (in oK) as % is inverse operation in k4 as opposed to square root
Scratch 3.0 24 23 blocks/239 228 bytes
Alternatively in SB syntax
when gf clicked
set[s v]to(0
ask()and wait
repeat until<(answer)=(
add(answer)to[m v
ask()and wait
end
set[n v]to(item(length of(n))of(m
repeat(length of((m)-(1
change[s v]by(item(1)of[m v
delete (1)of[m v
end
say(((n)/(s))*(100
Saved 11 bytes thanks to @JoKing
Answer History
Alternatively in SB syntax
when gf clicked
set[s v]to(0
ask()and wait
repeat until<(answer)=(
add(answer)to[m v
ask()and wait
end
set[n v]to(item(length of(n))of(m
delete(n)of[m v
repeat(length of(m
change[s v]by(item(1)of[m v
delete (1)of[m v
end
say(((n)/(s))*(100
Input is in the form of:
item1
item2
...
itemN
index
I really should stop doing this to myself. But it is very fun!
Stax, 7 bytes
â└╡Æå'W
Run and debug it at staxlang.xyz!
0-indexed. Requires at least one item in the input list to be of the form N.0 rather than simply N. Replacing / in the unpacked version with :_ gets around this restriction at the cost of one byte.
Unpacked (8 bytes) and explanation:
@AJ*x|+/ Index atop the stack, with the list beneath
@ Element at index...
AJ* Times 100
x|+ Sum of list
/ Divide
Bash, 77 75 73 72 80 76 75 bytes
a=($@)
((b=10000*${a[$1]}/(${@/%/+}-$1),b<10))
echo $[b/100].${?/1}$[b%100]
+8 bytes thanks to GammaFunction, who found a bug with large numbers
-5 bytes thanks to GammaFunction, who golfed his original bug fix
T-SQL 2008, 41 bytes
To find the row number equal to the index:
A division of index and row number multiplied with division of row number and index, if both results are 1, index and row are the same because both are above 0 and integer division round down. This saves 1 byte compared to using IIF
a - value
b - row number
Using table variable as input. 1-indexed
SELECT max(b/@*(@/b)*a*100)/sum(a)FROM @x
AWK, 43 40 bytes
{s+=a[NR]=$1}END{print 100*a[$1]/(s-$1)}
-3 bytes thanks to CowsQuack
Takes the index as the last line of input
Racket, 81 43 bytes
Accepts a list and index. Outputs rationals (default for division in Racket).
Big thanks to Galen Ivanov for finding the apply function. Still learning as I go :) .
(λ(l i)(*(/(list-ref l i)(apply + l))100))
PowerShell, 34 bytes
param($l,$n)$l|%{$i+=$_};$l[$n]/$i
Shame parameters are so dang expensive in Powershell.
Zsh, 32 30 bytes
-1 thanks to @ErikF, using 1e2 instead of 100..
<<<$[1e2*$@[`<&0`]/(${@/#/+})]
Accepts the list as arguments and the index on stdin.
<<<$[1e2*$@[`<&0`]/(${@/#/+})] # $[ arithmetic ]
$[ `<&0` ] # capture stdin
$[ $@[ ] ] # index into the arguments
$[1e2*$@[`<&0`] ] # multiply by 100.0 (casts to float)
$[ (${@/#/+})] # prepend each element with + and add
$[ / ] # divide
<<< # print to stdout
Swift, 60 bytes
{(n:[Int],i)->Float in
1e2*Float(n[i])/Float(n.reduce(0,+))}
I picked Swift to participate and bumped into its type sensitivity !
Batch, 111 bytes
@shift
@set s=%*
@call set/as=%s: =+%-%0,s=(%%%0*10000+s/2)/s,h=s%%%%10,t=s/10%%%%10,s/=100
@echo %s%.%t%%h%
Takes input as index and list as command-line arguments. Note: Only works for index values from 1 to 9 due to limitations of Batch; a 0-indexed version could be written which would be able to index the first 10 elements. Explanation:
@shift
Shift the index to %0 and the list to %1...%9 (or less). Note however that Batch's shift does not affect %*.
@set s=%*
Get all of the parameters, space separated.
@call set/as=%s: =+%-%0,s=(%%%0*10000+s/2)/s,h=s%%%%10,t=s/10%%%%10,s/=100
Change the spaces to +s and evaluate arithmetically, thus taking the sum, but subtract the index. Then index into the list, multiply by 10000, add half of the sum, and divide by the sum. Finally perform divmod by 10 twice to generate the decimal places. (The % arithmetic operator has special meaning in Batch and normally needs to be doubled but the call then requires a further doubling.)
@echo %s%.%t%%h%
Output the result and the decimal places.
Icon, 53 bytes
procedure f(L,i)
s:=0;s+:=!L&\z
return 1e2*L[i]/s
end
The only interesting thing here is finding the sum. Icon was one of the first languages to have generators. ! generates all the values of the list L that are accumulated to s. Normally we need to write every s+:=!L, but I used backtracking with &\z, which checks if the non-existent z variable is non-null, which is not, and extracts the next value from the list until exhaustion.
C (gcc), 64 bytes
0-indexed. The only fun bit was the realization that 1e2 is a double, saving a byte over 100.!
float f(v,n,t)int*v;{n=v[n];for(t=0;*v;t+=*v++);return n*1e2/t;}
Perl 6, 21 bytes
{100*@^a[$^b]/@a.sum}
The simple solution, since I can't use curried parameters with the $b parameter being indexed. A funner solution that doesn't have to handle two parameters by using the rotate function instead:
{100*.[0]/.sum}o&rotate
But it is unfortunately two bytes longer
MATL, 9 bytes
)1Gs/100*
Explanation
implicitly take two inputs
) get the entry within the first input at the index specified by the second
1G push the first input onto the stack again
s compute the sum
/ divide first entry of the stack by this number (the sum)
100* multiply by 100
JavaScript (ES6), 30 bytes
Takes input as (array)(index), where index is 0-based.
a=>n=>a[n]*100/eval(a.join`+`)
Retina 0.8.2, 102 bytes
\d+
$*
^(1)+((?<-1>.(1+))+)
$3$2
,
\G1
10000$*
;(1+)\1
$1;$1$1
r`.*(\2)*;(1+)
$#1
+`^..?$
0$&
..$
.$&
Try it online! Link includes test cases. Takes input as index;list,.... Explanation:
\d+
$*
Convert to unary.
^(1)+((?<-1>.(1+))+)
$3$2
Index into the list.
,
Sum the list.
\G1
10000$*
;(1+)\1
$1;$1$1
r`.*(\2)*;(1+)
$#1
Multiply the desired value by 10000 and divide by the sum with rounding by adding on half of the sum first.
+`^..?$
0$&
Ensure that the result has at least three digits.
..$
.$&
Insert a decimal point at the second last position.
TI-Basic, 12 bytes (12 tokens)
Prompt X
Ans(X)E2/sum(Ans
1-indexed
Takes the list in Ans and prompts the user for the index
Explanation:
Prompt X # Prompt the user for the index
Ans(X)E2/sum(Ans
Ans(X) # The value at the Xth index in the list
E2 # times 100
/sum(Ans # Divided by the sum of the list
# The result of the last expression in a program is implicitly returned
Haskell, 20 18 bytes
i?a=a!!i/sum a*100
A dyadic operator (?) taking the (0-indexed) index on the left and a list on the right which yields the percentage.
Python 3, 26 bytes
lambda i,a:a[i]/sum(a)*100
An unnamed function accepting an integer (0-indexed index) and a list which returns the percentage.
05AB1E, 6 bytes
è²O/т*
A full program taking the index then the list. Uses 0-indexing.
How?
è²O/т*
è - index (input 1) into (input 2)
² - push 2nd input
O - sum
/ - divide
т - push 100
* - multiply
- print top of stack
Jelly, 7 bytes
ị÷S}ȷ2×
A dyadic Link accepting an integer, one-based index on the left and a list of numbers on the right which yields the percentage.
How?
ị÷S}ȷ2× - Link: integer, i; list, L
ị - (i) index into (L)
} - use right argument:
S - sum (L)
÷ - divide
ȷ2 - literal 10^2 = 100
× - multiply
APL (Dyalog Unicode), 9 bytesSBCS
Anonymous tacit infix function. Takes index as left argument and list as right argument.
100×⌷÷1⊥⊢
100 one hundred
× times
⌷ the indexed element
÷ divided by
1⊥ the sum (lit. the base-1 evaluation) of
⊢ the right argument
Perl 5 -ap -MList::Util=Sum, 19 bytes
$_=100*$F[<>]/sum@F
Take the list, space separated on the first line, the index (0-based) on the second.

