g | x | w | all
Bytes Lang Time Link
049Tcl250523T172350Zsergiol
033AWK250521T153900Zxrs
036Rust230803T233321ZJeremy M
011Itr230803T191230Zbsoelch
006Thunno 2230705T190458ZThe Thon
nan230226T104218ZThe Thon
006Vyxal230226T065306Zlyxal
010Pyt230225T221911ZKip the
012CJam230221T203629Zemirps
041Erlang230221T214254ZYuri Gin
039PowerShell Core230221T211331ZJulian
079Snap!230220T140740Zqarz
006MathGolf190930T072449ZKevin Cr
023Arturo230220T110043Zchunes
079Go191001T175254ZS.S. Ann
040Clojure191107T133632ZThoma
013Pyth191107T011345Zfrank
061Forth gforth191017T065132ZBubbler
008Canvas191014T053415Zhakr14
014K oK190929T184403ZGalen Iv
014k4191004T140443Zscrawl
228Scratch 3.0191003T051740Zlyxal
007Stax191001T184921ZKhuldrae
075Bash190929T144133ZJonah
021Julia 1.0191001T150951ZSimeon S
041TSQL 2008191001T120325Zt-clause
040AWK190929T160959ZJonah
043Racket190929T231758ZMLavrent
034PowerShell190930T131429ZVeskah
030Zsh190929T190852ZGammaFun
060Swift190930T122040ZAZTECCO
111Batch190930T112723ZNeil
047Java JDK190930T092329ZOlivier
046Factor190930T080232ZGalen Iv
053Icon190930T074252ZGalen Iv
021Octave190930T063207Ztsh
029Red190929T070845ZGalen Iv
035PHP 7.4190929T034919ZNight2
064C gcc190930T002353ZErikF
021Perl 6190930T000355ZJo King
021Ruby190929T145359ZJonah
028R190929T113954Zniko
009MATL190929T120652Zflawr
030JavaScript ES6190929T001128ZArnauld
015Wolfram Language Mathematica190929T083630Zatt
102Retina 0.8.2190929T081132ZNeil
010J190929T071639ZGalen Iv
1212TIBasic190929T052537Zpizzapan
018Haskell190929T012533ZJonathan
026Python 3190929T005552ZJonathan
022C# Visual C# Interactive Compiler190929T005020ZGymhgy
00605AB1E190929T004424ZJonathan
008Japt190929T003923ZGymhgy
007Jelly190929T001931ZJonathan
009APL Dyalog Unicode190929T002830ZAdá
019Perl 5 ap MListUtil=Sum190929T002137ZXcali

Tcl, 49 bytes

proc P L\ n {expr [lindex $L $n]e2/([join $L +])}

Try it online!

AWK, 33 bytes

{for(;++i<NF;)x+=$i}$0=100*$$NF/x

Attempt This Online!

Rust, 36 bytes

|v,i|v[i]*100./v.iter().sum::<f32>()

0-indexed, assuming integers cast to floats before the function call

Rust Playground

Itr, 11 bytes

#ä#@áS/100*

zero-indexed, outputs a fraction

online Interpreter

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/ɦ×

Try it online!

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*

Attempt This Online!

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∑/₁*

Try it Online!

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ᴇ*

Try it online!

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*

Try it online!

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

Try it online!

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.

Try it online.

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)

Arturo, 23 bytes

$[a,i][100*a\[i]//∑a]

Try it

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.

Try it online!

Clojure, 40 bytes

(fn[a b](* 1e2(/(nth a b)(apply + a))))

Try it online!

It's nice to know about the 1e2 trick. :-)

Pyth, 13 bytes

c*100@hQeQshQ

Try it online!

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* ;

Try it online!

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××

Try it here!

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

K (oK), 15 14 bytes

-1 byte thanks to ngn!

{100*x[y]%+/x}

Try it online!

0-indexed

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

-11 bytes thanks to @JoKing

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

Try it on scratch

Answer History

Oh Scratchblocks, why so long?

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

Try it on scratch

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]

Try it online!

+8 bytes thanks to GammaFunction, who found a bug with large numbers

-5 bytes thanks to GammaFunction, who golfed his original bug fix

Julia 1.0, 21 bytes

f(l,i)=100l[i]/sum(l)

Try it online!

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

Try it online

AWK, 43 40 bytes

{s+=a[NR]=$1}END{print 100*a[$1]/(s-$1)}

Try it online!

-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))

Try it online!

PowerShell, 34 bytes

param($l,$n)$l|%{$i+=$_};$l[$n]/$i

Try it online!

Shame parameters are so dang expensive in Powershell.

Zsh, 32 30 bytes

-1 thanks to @ErikF, using 1e2 instead of 100..

<<<$[1e2*$@[`<&0`]/(${@/#/+})]

Try it online! Try it online!

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

Alternate 30 byte solution

Swift, 60 bytes

{(n:[Int],i)->Float in
1e2*Float(n[i])/Float(n.reduce(0,+))}

Try it online!

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.

Java (JDK), 47 bytes

a->i->1e2*a[i]/java.util.Arrays.stream(a).sum()

Try it online!

Factor, 46 bytes

: f ( i l -- n ) dup [ nth ] dip sum / 1e2 * ;

Try it online!

0-indexed

Icon, 53 bytes

procedure f(L,i)
s:=0;s+:=!L&\z
return 1e2*L[i]/s
end

Try it online!

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.

Octave, 21 bytes

@(a,n)a(n)/sum(a)*100

Try it online!

Red, 31 29 bytes

-2 bytes thanks to ErikF

func[b i][1e2 * b/:i / sum b]

Try it online!

PHP (7.4), 35 bytes

fn($l,$i)=>100/array_sum($l)*$l[$i]

Try it online!

Input index is 0-based.

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;}

Try it online!

Perl 6, 21 bytes

{100*@^a[$^b]/@a.sum}

Try it online!

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

Try it online!

But it is unfortunately two bytes longer

Ruby, 25 23 21 bytes

->a,i{1e2*a[i]/a.sum}

Try it online!

R 28 bytes

function(n,l)100*l[n]/sum(l)

Try it online!

MATL, 9 bytes

)1Gs/100*

Try it online!

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

Try it online!

JavaScript (ES6), 30 bytes

Takes input as (array)(index), where index is 0-based.

a=>n=>a[n]*100/eval(a.join`+`)

Try it online!

Wolfram Language (Mathematica), 15 bytes

100##[[]]/Tr@#&

Try it online!

Input as list, index

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.

J, 10 bytes

100*{%1#.]

Try it online!

0-indexed

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

Example run

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.

Try it online!

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.

Try it online!

C# (Visual C# Interactive Compiler), 22 bytes

x=>y=>x[y]*100/x.Sum()

Try it online!

05AB1E, 6 bytes

è²O/т*

A full program taking the index then the list. Uses 0-indexing.

Try it online!

How?

è²O/т*
è      - index (input 1) into (input 2)
 ²     - push 2nd input
  O    - sum
   /   - divide
    т  - push 100
     * - multiply
       - print top of stack

Japt, 8 bytes

gV
*L/Vx

Try it

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.

Try it online!

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⊥⊢

Try it online!

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

Try it online!

Take the list, space separated on the first line, the index (0-based) on the second.