
theorem NF997:
  for n being Nat, epsilon being Real,
  a being non empty positive at_most_one FinSequence of REAL st
  n is odd & len a = n & epsilon = 1 / (n + 1) &
  (for i being Nat st i in Seg n holds
  (i is odd  implies a . i = 2 * epsilon) &
  (i is even implies a . i = 1 - epsilon)) holds n = 2 * Opt a - 1
  proof
    let n be Nat, epsilon be Real,
    a be non empty positive at_most_one FinSequence of REAL;

    assume that
    L010: n is odd and
    L030: len a = n and
    L040: epsilon = 1 / (n + 1) and
    L050: for i being Nat st i in Seg n holds
    (i is odd  implies a . i = 2 * epsilon) &
    (i is even implies a . i = 1 - epsilon);

    L020: 1 <= n by L010,NF992;

    L060: (n + 1) div 2 = (n + 1) / 2 by L010,NF992;

    ex g being non empty FinSequence of NAT st
    dom g = dom a &
    (for j being Nat st j in rng g holds SumBin (a, g, {j}) <= 1) &
    (n + 1) div 2 = card rng g &
    (for f being non empty FinSequence of NAT st
    dom f = dom a &
    (for j being Nat st j in rng f holds SumBin (a, f, {j}) <= 1) holds
    (n + 1) div 2 <= card rng f)
    proof
      defpred P[Nat, object] means
      ($1 is odd  implies $2 = 1) &
      ($1 is even implies $2 = $1 div 2 + 1);

      L100: for i being Nat st i in Seg n holds ex x being object st P[i, x]
      proof
        let i be Nat;

        assume i in Seg n;

        per cases;
        suppose i is odd;
          hence thesis;
        end;
        suppose i is even;
          hence thesis;
        end;
      end;

      consider g0 being FinSequence such that
      L200: dom g0 = Seg n and
      L210: for i being Nat st i in Seg n holds P[i, g0 . i]
      from FINSEQ_1:sch 1 (L100);

      for i being Nat st i in dom g0 holds g0 . i in NAT
      proof
        let i be Nat;

        assume L309: i in dom g0;

        per cases;
        suppose i is odd; then
          g0 . i = 1 by L309,L200,L210;
          hence g0 . i in NAT;
        end;
        suppose i is even; then
          L370: g0 . i = i div 2 + 1 by L309,L200,L210;
          0 <= i div 2 by INT_1:55;
          hence g0 . i in NAT by INT_1:3,L370;
        end;
      end;
      then reconsider g0 as non empty FinSequence of NAT by NF315,L010,L200;

      take g0;

      L379: dom a = Seg n by FINSEQ_1:def 3,L030;

      thus dom g0 = dom a by FINSEQ_1:def 3,L030,L200;

      thus for j being Nat st j in rng g0 holds SumBin (a, g0, {j}) <= 1
      by L010,L030,L040,L050,L379,L200,L210,NF996;

      set npd2 = (n + 1) div 2;

      1 + 1 <= n + 1 by L010,NF992,XREAL_1:6;
      then L388: 2 / 2 <= (n + 1) / 2 by XREAL_1:72;

      reconsider npd2 as Nat by L010,L060;

      for y being object holds
      y in Seg npd2 iff ex x being object st x in dom g0 & y = g0 . x
      proof
        let y be object;

        hereby
          assume L405: y in Seg npd2;

          reconsider y0 = y as Nat by L405;

          1 <= y0 & y0 <= npd2 by L405,FINSEQ_1:1;
          then 1 + 1 <= y0 + 1 by XREAL_1:6;
          then 2 <= y0 or 2 = y0 + 1 by NAT_1:8;
          then per cases by L405,FINSEQ_1:1;
          suppose L410: y0 = 1;
            set x0 = 1;

            L415: x0 in Seg n by L020;

            L420: x0 in dom g0 by L020,L200;

            reconsider x0 as Nat;

            x0 mod 2 = 1 by NAT_D:14;
            then x0 is odd by NAT_2:22;
            then y0 = g0 . x0 by L415,L210,L410;
            hence ex x being object st x in dom g0 & y = g0 . x by L420;
          end;
          suppose L430: 2 <= y0 & y0 <= npd2;

            set x0 = (y0 - 1) * 2;

            2 - 1 <= y0 - 1 by L430,XREAL_1:9;
            then 1 * 2 <= (y0 - 1) * 2 by XREAL_1:64;
            then L4125: 2 - 1 <= x0 - 0 by XREAL_1:13;

            reconsider x0 as Nat by L430;

            x0 div 2 = ((y0 - 1) * 2) / 2 by NAT_6:4;
            then L412: y0 = x0 div 2 + 1;

            y0 - 1 <= (n + 1) / 2 - 1 by L430,L060,XREAL_1:9;
            then x0 <= ((n + 1) / 2 - 1) * 2 by XREAL_1:64;
            then L4135: x0 + 0 <= n - 1 + 1 by XREAL_1:7;
            then L415: x0 in Seg n by L4125;

            L420: x0 in dom g0 by L4125,L4135,L200;

            y0 = g0 . x0 by L415,L210,L412;
            hence ex x being object st x in dom g0 & y = g0 . x by L420;
          end;
        end;

        given x0 being object such that
        L440: x0 in dom g0 and
        L442: y = g0 . x0;

        reconsider x0 as Nat by L440;

        per cases;
        suppose x0 is odd;
          then g0 . x0 = 1 by L440,L200,L210;
          hence y in Seg npd2 by L442,L388,L060;
        end;
        suppose L470: x0 is even;
          then g0 . x0 = x0 div 2 + 1 by L440,L200,L210;
          then L472: y = x0 / 2 + 1 by L442,L470,NAT_6:4;

          reconsider y0 = y as Nat by L442;

          L4725: 0 + 1 <= x0 / 2 + 1 by XREAL_1:6;

          L474: x0 <= n by L440,L200,FINSEQ_1:1;

          x0 + 1 <= n or not x0 + 1 <= n + 1 by L010,L470,NAT_1:8;
          then x0 + 1 + 1 <= n + 1 by L474,XREAL_1:6;
          then (x0 + 2) / 2 <= (n + 1) / 2 by XREAL_1:72;
          hence y in Seg npd2 by L4725,L442,L060,L472;
        end;
      end;
      then rng g0 = Seg npd2 by FUNCT_1:def 3;
      hence (n + 1) div 2 = card rng g0 by FINSEQ_1:57;

      reconsider i0 = (n + 1) / 2 as Integer by L010;
      reconsider r = 1 / (n + 1) - 1 / 2 as Real;

      L810: Sum a
       = (n + 1) / 2 + 1 / (n + 1) - 1 / 2 by L010,L030,L040,L050,NF994
      .= r + i0;

      1 + 1 <= n + 1 by L010,NF992,XREAL_1:6;
      then 1 * 2 <= 1 * (n + 1);
      then 1 / (n + 1) <= 1 / 2 by XREAL_1:102;
      then L820: r <= 0 by XREAL_1:47;

      0 < 1 / (n + 1) + 1 / 2 by XREAL_1:34;
      then 0 < r + 1;
      then L835: [/ r \] = 0 by L820,INT_1:def 7;

      [/ r + i0 \] = [/ r \] + i0 by INT_1:33
      .= i0 by L835; then

      [/ Sum a \] = (n + 1) div 2 by L810,L010,NF992;
      hence for f being non empty FinSequence of NAT st dom f = dom a &
      (for j being Nat st j in rng f holds SumBin (a, f, {j}) <= 1)
      holds (n + 1) div 2 <= card rng f by NF320;
    end;
    then Opt a = (n + 1) div 2 by defOpt;
    hence n = 2 * Opt a - 1 by L060;
  end;
