# Counterexample to Shapiro inequality

Input: A positive integer n such that n is even and greater than 12 or n is odd and greater than 23.

Output: A list of non-negative integers that violates Shapiro inequality.

More precisely, Let s be a list of non-negative integers such that the following code does not cause any errors (assume s has already been defined):

from fractions import Fraction
y=0
n=len(s)
for x in range(n):
assert type(s[x]) is int
assert s[x]>=0
y+=Fraction(s[x], s[(x+1)%n]+s[(x+2)%n])
assert y*2<n


Winner is shortest code. Standard loopholes apply. Shapiro inequality is: $$\sum_{x=1}^n\frac{s_x}{s_{x+1}+s_{x+2}}\ge\frac{n}{2}\text{ where } s_{n+1} = s_1\text{ and } s_{n+2}=s_2.$$

$$\text{You want }\sum_{x=1}^n\frac{s_x}{s_{x+1}+s_{x+2}}<\frac{n}{2}\text{ where } s_{n+1} = s_1\text{ and } s_{n+2}=s_2.$$

Test case:

Input: 14
Possible output: [0,42,2,42,4,41,5,39,4,38,2,38,0,40]


Note that $$\\frac0{42+2}+\frac{42}{2+42}+\frac{2}{42+4}+\dots+\frac{40}{0+42}\approx6.999995<14/2\$$.

New contributor
Lucenaposition is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• Could you explain the Shapiro inequality in words?
– Tbw
Commented Jul 31 at 1:08
• It's hard to explain without using math symbols. Commented Jul 31 at 1:15
• Care to explain the downvotes? Commented Jul 31 at 9:39
• Welcome to Code Golf and nice first question! For future reference, we recommend using the Sandbox to get feedback on challenge ideas before posting them. I can't be certain about the downvotes, but I do have some tips. I think the biggest help right away would be to include some test cases showing a few possible inputs and some possible valid outputs corresponding to them. Commented Jul 31 at 17:44
• [Cont.] I think also this challenge could use a little more clarity, I think what I'd do is fully define the Shapiro inequality, even if just copy pasting it from wikipedia, then explain the challenge (generating counterexamples), then talk about what the i/o specs are, and then have example code/test cases. Headers can help distinguish these sections. It may also be worthwhile to include a worked example (take a test case and fully show how both sides of the inequality are calculated). These are not all strictly necessary, but they are some common things that could improve readability Commented Jul 31 at 18:23

My first codegolf on this form--

n=gets.to_i;s=[1]*n;loop{s[0]+=1;y=0;n.times{|i|y+=s[i].to_f/(s[(i+1)%n]+s[(i+2)%n])};break if y*2>=n};p s


Total 107 bytes, not much better than the 168 bytes provided in the problem statement but my best shot!

1. Read the input value --> initalize an array s of size n (the input) with all 1s.

2. Enter an infinite loop --> first element of array s is incremented, then sum of specific fractions is calculated and if the sum (multiplied by 2) exceeds n then the loop breaks and prints s.

s = [1] * n

loop do
s[0] += 1
y = 0

n.times do |i|
y += s[i].to_f / (s[(i + 1) % n] + s[(i + 2) % n])
end

break if y * 2 >= n
end

p s

New contributor
Dimitri Chrysafis is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• This is Ruby, right? You should make a title with the language name and code length Commented Jul 31 at 5:03
• Note that you need to violate the inequality, not satisfy it Commented Jul 31 at 5:16
• Hi and welcome to CGCC! Answers are generally posted using a set template, that includes the language and byte count in the title, and the template also often includes a link to an online interpreter. Here's one for ruby. You can then generate a template for your post using the blue button in the top right of the page, choosing "CGCC Post". If you paste your code there, it will also be pasted for people who use the generated link. Commented Jul 31 at 8:21