2

I have this problem with generating Gauss Numbers. The problem is like this :

enter image description here

This is what i coded so far

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SirurileLuiGauss
{
    class Program
    {
        static int[] a = new int[100];
        static int[] b = new int[100];

        static List<int> list_a = new List<int>();
        static List<int> list_b = new List<int>();

        static int GaussA(int n)
        {
            if(n <= 0)
            {
                int a = 0;
                list_a.Add(a);

                return a;
            }
            else
            {
                int a = (GaussA(n - 1) + GaussB(n - 1))/2;
                list_a.Add(a);

                return a;
            }
        }

        static int GaussB(int n)
        {
            if(n <= 0)
            {
                int b = 0;
                list_b.Add(b);

                return b;
            }
            else
            {
                int b = (int) Math.Sqrt(GaussA(n - 1) * GaussB(n - 1));
                list_b.Add(b);

                return b;
            }
        }


        static void Main(string[] args)
        {
            int n = 5;

            Console.WriteLine("GAUSS_111");
            GaussA(n);

            foreach (int element in list_a)
            {
                Console.WriteLine("GAUSS_A = "+element);
            }

            foreach (int element in list_b)
            {
                Console.WriteLine("GAUSS_B = " + element);
            }


            Console.WriteLine("\n\n");

            // Clear the list
            list_a = new List<int>();
            list_b = new List<int>();

            Console.WriteLine("GAUSS_222");
            GaussB(n);

            foreach (int element in list_a)
            {
                Console.WriteLine("GAUSS_A = " + element);
            }

            foreach (int element in list_b)
            {
                Console.WriteLine("GAUSS_B = " + element);
            }

            Console.Read();
        }
    }
}

And is totally wrong, gives me the output like 0 0 0 0 ...

  1. What am i doing wrong?
  2. Can i find this anywhere? Like a library or something?
  3. I thing that the approach of this is by computing the integral and find out what is the peak/max_number of the array. Then try to iterate backwards to find all the numbers until we hit a > 0 and b > 0. The main problem is that you have to keep 2 arrays into memory, and that is hard ... Am i correct?
  4. How do i compute that integral in C# ?
  5. Why do you need epsilon for precision?

Thank you very much.

ᴜsᴇʀ
  • 1,109
  • 2
  • 9
  • 23
Master345
  • 2,250
  • 11
  • 38
  • 50
  • possible duplicate of [Generate a random number in a Gaussian Range?](http://stackoverflow.com/questions/5281672/generate-a-random-number-in-a-gaussian-range) – Liam Jan 22 '14 at 16:56
  • As far as I can see if a[i] = 0 and b[i] = 0 than a[n] = 0 as well as b[n] = 0; you've initialize a = 0; and b = 0; and so it's your case. Yoi should have GaussA(Double a, int n) not just GaussA(int n) the same with GaussB method – Dmitry Bychenko Jan 22 '14 at 16:57
  • May sound like `gussian range` ... but i might not be, as i said => I think that the approach of this is by computing the integral and find out what is the peak/max_number of the array. Then try to iterate backwards to find all the numbers until we hit a > 0 and b > 0. The main problem is that you have to keep 2 arrays into memory, and that is hard ... Am i correct? – Master345 Jan 22 '14 at 17:34

1 Answers1

1

The problem starts as "let a0 = ... b0 = ..."; so you need a0 as well as b0 being input arguments. Another issue is that you don't need any array or list here (imagine, that you're asked to find out a millionth itteration), dynamic programming is a far better choice here:

   // I let myselft return the result as Tupple<,>
   public static Tuple<Double, Double> Gauss(Double a0, Double b0, int n) {
      Double prior_a;
      Double prior_b;

      Double a = a0;
      Double b = b0;

      for (int i = 0; i < n; ++i) {
        prior_a = a;
        prior_b = b;

        a = (prior_a + prior_b) / 2.0;
        b = Math.Sqrt(prior_a * prior_b);
      }

      return new Tuple<Double, Double>(a, b);
    }

Simple test: let a0 = 1; b0 = 5, so we have

Theory:
  itt #  a             b
      0  1             5
      1  3             sqrt(5)
      2  (3+sqrt(5))/2 sqrt(3*sqrt(5))

Actual:

  Gauss(1, 5, 0);   // returns (1, 5)
  Gauss(1, 5, 1);   // -/-     (3, 2.23606797749979)
  Gauss(1, 5, 2);   // -/-     (2.61803398874989, 2.59002006411135)
  Gauss(1, 5, 3);   // -/-     (2.60402702643062, 2.60398935469938)
  Gauss(1, 5, 10);  // -/-     (2.60400819053094, 2.60400819053094)
  Gauss(1, 5, 100); // -/-     (2.60400819053094, 2.60400819053094)

P.S. You can't compute (get, say, double value) an indefinite integral cause it equals F(x) + C where C is arbitrary constant; for definite integtral you may use Simpson algorithm

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Thank you for the response, but the output of this is `1 1 1` for `F(1,5)` or `3 3 3` for `F(3,5)` ... and i think this has to be a recursive function, this is how it says here in the book ... – Master345 Jan 22 '14 at 17:33
  • @Master345: Sorry, but Gauss(a, b, n) meets with theoretical values (see my edit - test for Gauss(1, 5, n)), Gauss(a, b, n) quickly converges as expected (it requires about 10 itterations only for abs(a(n) - b(n)) < Double.Espilone). The recursion as you can see is already in the "for" loop. What is F(1, 5)? – Dmitry Bychenko Jan 23 '14 at 06:13
  • So there must not be two functions? Or the `Tuple` does the job? – Master345 Feb 02 '14 at 20:05
  • @Master345: The number of functions (one or two) is up to you; IMHO one function (which returns two values, however) would do better – Dmitry Bychenko Feb 03 '14 at 05:55