Beispiel #1
0
        public AccurateFactorial Accurate(AccurateFactorial n)  //Most accurate method
        //This method uses arrays instead of numbers
        {
            n = n.DisposeZero(n);
            if (!n.BigEnough(n))    //Check if the number is higher than 1
            {
                if (n.value[0] > 0)
                {
                    return(n);
                }
                else
                {
                    return(new AccurateFactorial(1));    //only returns if the number is zero
                }
            }
            // If I used "return n.multiply(Accurate(n.previous(n)), n)"
            //the program worked much slower
            AccurateFactorial m = new AccurateFactorial(n.value);

            m.value = n.value;
            m       = n.previous(m);
            AccurateFactorial m2 = new AccurateFactorial(Accurate(m).value);

            return(n.multiply(m2, n));
        }
Beispiel #2
0
 //Check if the number is bigger than 1
 public bool BigEnough(AccurateFactorial n)
 {
     if (n.value != null)
     {
         if (n.value.Length > 1 || n.value[0] > 1)
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #3
0
        //Get multiplication of two big numbers
        public AccurateFactorial multiply(params AccurateFactorial[] af)
        {
            AccurateFactorial mult = new AccurateFactorial();

            for (int k = 0; k < 2; k++)
            {
                DisposeZero(af[0]);                        //prepare the numbers to work
            }
            //Set the size of new array
            int length = af[0].value.Length + af[1].value.Length,
                len1 = af[0].value.Length - 1, len2 = af[1].value.Length - 1;

            mult.SetSize(length);
            //implement the multiplication algorithm
            length--; int reminder  = 0;
            int           MaxLength = length;

            for (int i = len1; i >= 0; i--)
            {
                for (int k = len2; k >= 0; k--)
                {
                    int temp = reminder;
                    temp                  = af[0].value[i] * af[1].value[k];
                    reminder             += temp / 10;
                    mult.value[length]   += temp % 10;
                    mult.value[--length] += reminder;
                    reminder              = 0;
                }
                MaxLength--;
                length = MaxLength;
            }
            reminder = 0;
            length   = mult.value.Length - 1;
            //Final summarization
            for (int i = length; i > 0; i--)
            {
                mult.value[i - 1] += mult.value[i] / 10;
                mult.value[i]      = mult.value[i] % 10;
            }
            mult = DisposeZero(mult);
            //Result
            return(mult);
        }
Beispiel #4
0
        //Dispose from leading zeros
        public AccurateFactorial DisposeZero(AccurateFactorial n)
        {
            if (n.value.Length > 1)
            {
                int len = 0, i = 0;
                while (i < n.value.Length - 1 && n.value[i] == 0)
                {
                    len++;
                    i++;
                }
                AccurateFactorial temp = new AccurateFactorial();

                temp.SetSize(n.value.Length - len);
                for (i = 0; i < n.value.Length - len; i++)
                {
                    temp.value[i] = n.value[len + i];
                }
                return(temp);
            }
            return(n);
        }
Beispiel #5
0
        //Get previous number
        public AccurateFactorial previous(AccurateFactorial n4)
        {
            if (!BigEnough(n4))
            {
                return(n4);
            }
            AccurateFactorial prev = new AccurateFactorial(n4.value);
            int i = prev.Size() - 1;

            if (i > -1)
            {
                while (i > 0 && prev.value[i] < 1)
                {
                    prev.value[i]--;
                    prev.value[i] = (prev.value[i] + 10) % 10;
                    i--;
                }
                prev.value[i]--;
                return(prev);
            }
            prev.value[i]--;
            return(prev);
        }
Beispiel #6
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (checkedListBox1.CheckedIndices.Count == 0)      //check if algorithm is been chosen
            {
                MessageBox.Show("Choose algorithm!");
                return;
            }
            if (textBox1.Text.Length < 1) //check if the represented number string is not empty
            {
                MessageBox.Show("Type integer number!");
                return;
            }
            string checkstring = "0123456789";

            try
            {
                //Check if the string only consists of the numerals
                int SymbolsCount = 0;
                foreach (char c in checkstring)
                {
                    for (int i = 0; i < textBox1.Text.Length; i++)
                    {
                        if (textBox1.Text[i] == c)
                        {
                            SymbolsCount++;
                        }
                    }
                }
                if (SymbolsCount == textBox1.Text.Length)
                {
                    AccurateFactorial n = new AccurateFactorial(textBox1.Text);
                    n             = n.DisposeZero(n); //Dispose of extra zeros in the beginning
                    textBox1.Text = n.ToString();
                    //Choosing different methods depending on the selected algorithm
                    if (checkedListBox1.CheckedIndices[0] != 5)
                    {
                        if (long.TryParse(textBox1.Text, out long m))
                        {
                            switch (checkedListBox1.CheckedIndices[0])
                            {
                            case 0:
                                textBox2.Text = Factorial.GetFactorialWithCycles(m).ToString();
                                break;

                            case 1:
                                textBox2.Text = Factorial.GetFactorial(m).ToString();
                                break;

                            case 2:
                                textBox2.Text = Factorial.Approximation(m, "Ramanujan").ToString();
                                break;

                            case 3:
                                textBox2.Text = Factorial.Approximation(m, "Stirling").ToString();
                                break;

                            case 4:
                                textBox2.Text = Factorial.Approximation(m, "").ToString();
                                break;
                            }
                            if (long.TryParse(textBox2.Text, out long k))
                            {
                                if (k < 1 || m > 20)
                                {//Coud not calculate the factorial
                                    textBox2.Text = "Error";
                                    MessageBox.Show("The number is too big for the current method");
                                    return;
                                }
                            }
                        }
                        return;
                    }
                    //check if the number is small enough
                    if (n.value.Length > 4 || (n.value.Length > 3 && n.value[0] > 6 && n.value[1] > 7))
                    {
                        textBox2.Text = $"The number {n.ToString()} is too big integer number (>=7500)";
                        return;
                    }
                    Factorial f = new Factorial();                              //I made all methods to be used in the Factorial class
                    n             = new AccurateFactorial(f.Accurate(n).value); //calculate using the best method
                    textBox2.Text = n.ToString();                               //write result
                }
                else
                {
                    textBox2.Text = "Error: the string must contain only numerals";
                }
            }
            catch (Exception) {
                MessageBox.Show("Unkown Error");
            }
        }