Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

66%
+2 −0
Challenges Expected value of highest dice rolled

Dyalog APL, 14 bytes {6-+/⍵*⍨6÷⍨⍳5} Not bruteforce! An exact implementation of the formula \[ E_n = 6 - \sum_{i=1}^5 \left(\frac i 6\right)^n \] 6- 6 minus +/ the sum of 6÷⍨⍳5 the list 1/...

posted 10mo ago by RubenVerg‭  ·  edited 10mo ago by RubenVerg‭

Answer
#3: Post edited by user avatar RubenVerg‭ · 2023-07-09T11:34:16Z (10 months ago)
  • # Dyalog APL, 14 bytes
  • ```apl
  • {6-+/⍵*⍨6÷⍨⍳5}
  • ```
  • Not bruteforce! An exact implementation of the formula
  • \[
  • E_n = 6 - \sum_{i=1}^5 \left(\frac i 6\right)^n
  • \]
  • * `6-` 6 minus
  • * `+/` the sum of
  • * `6÷⍨⍳5` the list 1/6, 2/6, 3/6, 4/6, 5/6
  • * `⍵*⍨` to the power of the argument of the function
  • ## Formula explanation
  • For simplicity, let's set $n=2$. This process trivially expands to higher dimensions.
  • If we roll 2 dice, this table represents all the possible pairs:
  • ```text
  • ⍳6 6
  • ┌───┬───┬───┬───┬───┬───┐
  • │1 1│1 2│1 3│1 4│1 5│1 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │2 1│2 2│2 3│2 4│2 5│2 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │3 1│3 2│3 3│3 4│3 5│3 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │4 1│4 2│4 3│4 4│4 5│4 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │5 1│5 2│5 3│5 4│5 5│5 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │6 1│6 2│6 3│6 4│6 5│6 6│
  • └───┴───┴───┴───┴───┴───┘
  • ```
  • Let's take the maximum of each pair:
  • ```text
  • ⌈/¨⍳6 6
  • 1 2 3 4 5 6
  • 2 2 3 4 5 6
  • 3 3 3 4 5 6
  • 4 4 4 4 5 6
  • 5 5 5 5 5 6
  • 6 6 6 6 6 6
  • ```
  • Notice that if we sum the whole table and divide by $6^n$, we get our desired expected value
  • ```text
  • 36÷⍨+/,⌈/¨⍳6 6
  • 4.472222222
  • ```
  • Alright, so we don't *really* care about the table, just its sum. I'll print it alongside the table as we build it.
  • ```text
  • (P←{⍵(+/,⍵)})⌈/¨⍳6 6
  • ┌───────────┬───┐
  • │1 2 3 4 5 6│161│
  • │2 2 3 4 5 6│ │
  • │3 3 3 4 5 6│ │
  • │4 4 4 4 5 6│ │
  • │5 5 5 5 5 6│ │
  • │6 6 6 6 6 6│ │
  • └───────────┴───┘
  • ```
  • Let's find another way to build the same table (sum). Start with a table of all 6's.
  • ```text
  • P 6 6⍴6
  • ┌───────────┬───┐
  • │6 6 6 6 6 6│216│
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • └───────────┴───┘
  • ```
  • We want to subtract 1 from all entries except for those in the last row or column. Alternatively, we want to subtract this table from ours:
  • ```table
  • ~6∊¨⌈/¨⍳6 6
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 0 0 0 0 0 0
  • ```
  • This is a $5\times 5$ square.
  • We now get the following table:
  • ```text
  • P (6 6⍴6)-(~6∊¨⌈/¨⍳6 6)
  • ┌───────────┬───┐
  • │5 5 5 5 5 6│191│
  • │5 5 5 5 5 6│ │
  • │5 5 5 5 5 6│ │
  • │5 5 5 5 5 6│ │
  • │5 5 5 5 5 6│ │
  • │6 6 6 6 6 6│ │
  • └───────────┴───┘
  • ```
  • Repeating the same process for squares of side 4, 3, 2 and 1, we get exactly the original table (and sum!) we wanted.
  • Expressed mathematically, the expected value can therefore be written as
  • \[
  • \frac 1 {6^n} \left(6\cdot6^n - 5^n - 4^n - 3^n - 2^n - 1^n ight) = 6 - \sum_{i=0}^5 \left(\frac i 6 ight)^n
  • \]
  • # Dyalog APL, 14 bytes
  • ```apl
  • {6-+/⍵*⍨6÷⍨⍳5}
  • ```
  • Not bruteforce! An exact implementation of the formula
  • \[
  • E_n = 6 - \sum_{i=1}^5 \left(\frac i 6\right)^n
  • \]
  • * `6-` 6 minus
  • * `+/` the sum of
  • * `6÷⍨⍳5` the list 1/6, 2/6, 3/6, 4/6, 5/6
  • * `⍵*⍨` to the power of the argument of the function
  • ## Formula explanation
  • For simplicity, let's set $n=2$. This process trivially expands to higher dimensions.
  • If we roll 2 dice, this table represents all the possible pairs:
  • ```text
  • ⍳6 6
  • ┌───┬───┬───┬───┬───┬───┐
  • │1 1│1 2│1 3│1 4│1 5│1 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │2 1│2 2│2 3│2 4│2 5│2 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │3 1│3 2│3 3│3 4│3 5│3 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │4 1│4 2│4 3│4 4│4 5│4 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │5 1│5 2│5 3│5 4│5 5│5 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │6 1│6 2│6 3│6 4│6 5│6 6│
  • └───┴───┴───┴───┴───┴───┘
  • ```
  • Let's take the maximum of each pair:
  • ```text
  • ⌈/¨⍳6 6
  • 1 2 3 4 5 6
  • 2 2 3 4 5 6
  • 3 3 3 4 5 6
  • 4 4 4 4 5 6
  • 5 5 5 5 5 6
  • 6 6 6 6 6 6
  • ```
  • Notice that if we sum the whole table and divide by $6^n$, we get our desired expected value
  • ```text
  • 36÷⍨+/,⌈/¨⍳6 6
  • 4.472222222
  • ```
  • Alright, so we don't *really* care about the table, just its sum. I'll print it alongside the table as we build it.
  • ```text
  • (P←{⍵(+/,⍵)})⌈/¨⍳6 6
  • ┌───────────┬───┐
  • │1 2 3 4 5 6│161│
  • │2 2 3 4 5 6│ │
  • │3 3 3 4 5 6│ │
  • │4 4 4 4 5 6│ │
  • │5 5 5 5 5 6│ │
  • │6 6 6 6 6 6│ │
  • └───────────┴───┘
  • ```
  • Let's find another way to build the same table (sum). Start with a table of all 6's.
  • ```text
  • P 6 6⍴6
  • ┌───────────┬───┐
  • │6 6 6 6 6 6│216│
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • └───────────┴───┘
  • ```
  • We want to subtract 1 from all entries except for those in the last row or column. Alternatively, we want to subtract this table from ours:
  • ```table
  • ~6∊¨⌈/¨⍳6 6
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 0 0 0 0 0 0
  • ```
  • This is a $5\times 5$ square.
  • We now get the following table:
  • ```text
  • P (6 6⍴6)-(~6∊¨⌈/¨⍳6 6)
  • ┌───────────┬───┐
  • │5 5 5 5 5 6│191│
  • │5 5 5 5 5 6│ │
  • │5 5 5 5 5 6│ │
  • │5 5 5 5 5 6│ │
  • │5 5 5 5 5 6│ │
  • │6 6 6 6 6 6│ │
  • └───────────┴───┘
  • ```
  • Repeating the same process for squares of side 4, 3, 2 and 1, we get exactly the original table (and sum!) we wanted.
  • Expressed mathematically, the expected value can therefore be written as
  • \[
  • \frac 1 {6^n} \left(6\cdot6^n - 5^n - 4^n - 3^n - 2^n - 1^n ight) = 6 - \sum_{i=1}^5 \left(\frac i 6 ight)^n
  • \]
#2: Post edited by user avatar RubenVerg‭ · 2023-07-08T22:48:40Z (10 months ago)
add explanation, change code slightly
  • # Dyalog APL, 14 bytes
  • ```apl
  • {7-+/⍵*⍨6÷⍨⍳6}
  • ```
  • Not bruteforce! An exact implementation of the formula
  • \[
  • E_n = 7 - \sum_{i=1}^6 \left(\frac i 6 ight)^n
  • \]
  • * `7-` 7 minus
  • * `+/` the sum of
  • * `6÷⍨⍳6` the list 1/6, 2/6, 3/6, 4/6, 5/6, 6/6
  • * `⍵*⍨` to the power of the argument of the function
  • # Dyalog APL, 14 bytes
  • ```apl
  • {6-+/⍵*⍨6÷⍨⍳5}
  • ```
  • Not bruteforce! An exact implementation of the formula
  • \[
  • E_n = 6 - \sum_{i=1}^5 \left(\frac i 6 ight)^n
  • \]
  • * `6-` 6 minus
  • * `+/` the sum of
  • * `6÷⍨⍳5` the list 1/6, 2/6, 3/6, 4/6, 5/6
  • * `⍵*⍨` to the power of the argument of the function
  • ## Formula explanation
  • For simplicity, let's set $n=2$. This process trivially expands to higher dimensions.
  • If we roll 2 dice, this table represents all the possible pairs:
  • ```text
  • ⍳6 6
  • ┌───┬───┬───┬───┬───┬───┐
  • │1 1│1 2│1 3│1 4│1 5│1 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │2 1│2 2│2 3│2 4│2 5│2 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │3 1│3 2│3 3│3 4│3 5│3 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │4 1│4 2│4 3│4 4│4 5│4 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │5 1│5 2│5 3│5 4│5 5│5 6│
  • ├───┼───┼───┼───┼───┼───┤
  • │6 1│6 2│6 3│6 4│6 5│6 6│
  • └───┴───┴───┴───┴───┴───┘
  • ```
  • Let's take the maximum of each pair:
  • ```text
  • ⌈/¨⍳6 6
  • 1 2 3 4 5 6
  • 2 2 3 4 5 6
  • 3 3 3 4 5 6
  • 4 4 4 4 5 6
  • 5 5 5 5 5 6
  • 6 6 6 6 6 6
  • ```
  • Notice that if we sum the whole table and divide by $6^n$, we get our desired expected value
  • ```text
  • 36÷⍨+/,⌈/¨⍳6 6
  • 4.472222222
  • ```
  • Alright, so we don't *really* care about the table, just its sum. I'll print it alongside the table as we build it.
  • ```text
  • (P←{⍵(+/,⍵)})⌈/¨⍳6 6
  • ┌───────────┬───┐
  • │1 2 3 4 5 6│161│
  • │2 2 3 4 5 6│ │
  • │3 3 3 4 5 6│ │
  • │4 4 4 4 5 6│ │
  • │5 5 5 5 5 6│ │
  • │6 6 6 6 6 6│ │
  • └───────────┴───┘
  • ```
  • Let's find another way to build the same table (sum). Start with a table of all 6's.
  • ```text
  • P 6 6⍴6
  • ┌───────────┬───┐
  • │6 6 6 6 6 6│216│
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • │6 6 6 6 6 6│ │
  • └───────────┴───┘
  • ```
  • We want to subtract 1 from all entries except for those in the last row or column. Alternatively, we want to subtract this table from ours:
  • ```table
  • ~6∊¨⌈/¨⍳6 6
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 1 1 1 1 1 0
  • 0 0 0 0 0 0
  • ```
  • This is a $5\times 5$ square.
  • We now get the following table:
  • ```text
  • P (6 6⍴6)-(~6∊¨⌈/¨⍳6 6)
  • ┌───────────┬───┐
  • │5 5 5 5 5 6│191│
  • │5 5 5 5 5 6│ │
  • │5 5 5 5 5 6│ │
  • │5 5 5 5 5 6│ │
  • │5 5 5 5 5 6│ │
  • │6 6 6 6 6 6│ │
  • └───────────┴───┘
  • ```
  • Repeating the same process for squares of side 4, 3, 2 and 1, we get exactly the original table (and sum!) we wanted.
  • Expressed mathematically, the expected value can therefore be written as
  • \[
  • \frac 1 {6^n} \left(6\cdot6^n - 5^n - 4^n - 3^n - 2^n - 1^n\right) = 6 - \sum_{i=0}^5 \left(\frac i 6\right)^n
  • \]
#1: Initial revision by user avatar RubenVerg‭ · 2023-07-08T09:38:19Z (10 months ago)
# Dyalog APL, 14 bytes

```apl
{7-+/⍵*⍨6÷⍨⍳6}
```

Not bruteforce! An exact implementation of the formula

\[
E_n = 7 - \sum_{i=1}^6 \left(\frac i 6\right)^n
\]

* `7-` 7 minus
* `+/` the sum of
* `6÷⍨⍳6` the list 1/6, 2/6, 3/6, 4/6, 5/6, 6/6
* `⍵*⍨` to the power of the argument of the function