Left in sandbox for at least 3 days.
I want to verify if this inequality is true:
for \$n\geq4\$, if \$a_1,a_2,a_3,\dots,a_n\in R_+\cup\{0\}\$ and \$\sum_{i=1}^na_i=1\$, then \$a_1a_2+a_2a_3+a_3a_4+\dots+a_{n-1}a_n+a_na_1\leq\frac{1}{4}\$.
Challenge
Write a piece of program which takes an integer n
as input. It does the following:
- Generate a random array
a
which consists ofn
non-negative reals. The sum of all elements should be 1.
By saying random, I mean, every array satisfiying the requirements in 2 should have a non-zero probability of occurrence. It don't need to be uniform. See this related post.
Calculate
a[0]a[1]+a[1]a[2]+a[2]a[3]+...+a[n-2]a[n-1]+a[n-1]a[0]
.Output the sum and the array
a
.
For I/O forms see this post.
Rules
(Sorry for the late edit...) All numbers should be rounded to at least \$10^{-4}\$.
Standard loopholes should be forbidden.
Example
The following code is an ungolfed Python code for this challenge, using library numpy
. (For discussion about using libraries, see This Link.)
import numpy as np
def inequality(n):
if n < 4:
raise Exception
a = np.random.rand(n)
sum_a = 0
for i in range(n):
sum_a += a[i]
for i in range(n):
a[i] /= sum_a
sum_prod = 0
for i in range(n):
sum_prod += a[i % n] * a[(i + 1) % n]
print(a)
return sum_prod, a
Tip
You could assume that input n
is a positive integer greater than 3.
Your score is the bytes in your code. The one with the least score wins.