Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / F#

BigInteger Square Root in F#

Rate me:
Please Sign up or sign in to vote.
4.23/5 (6 votes)
19 Oct 2011CPOL5 min read 38.5K   338   10  
Determine the square root of a BigInteger using F#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Numerics;

namespace GUI
{
    public partial class SqrtForm : Form
    {
        private BigInteger original;  // The number to take the square root of.
        private BigInteger answer;    // To hold the answer to bigintSqrt(original)

        public SqrtForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Resize and position the controls based on the news size of the window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SqrtForm_Resize(object sender, EventArgs e)
        {
            txtOriginalInt.Height = (this.Height - 150) / 2;
            sqrtButton.Location = new Point(sqrtButton.Location.X, txtOriginalInt.Location.Y + txtOriginalInt.Height + 15);
            lblAnswer.Location = new Point(lblAnswer.Location.X, sqrtButton.Location.Y + 44);
            txtSqrtAnswer.Location = new Point(txtSqrtAnswer.Location.X, sqrtButton.Location.Y + 63);
            txtSqrtAnswer.Height = txtOriginalInt.Height;  
        }

        /// <summary>
        /// Determine if text in the txtOriginalInt control can be parsed as a positive integer.
        /// </summary>
        /// <returns>True if the text is parsed a positive integer, else False.</returns>
        private bool IsParsedAsPositiveInt()
        {
            sqrtErrorProvider.Clear();
            bool notValid = ! BigInteger.TryParse(txtOriginalInt.Text, out original);
            if (notValid)
            {
                sqrtErrorProvider.SetError(txtOriginalInt, "Please enter a positive integer");
                txtSqrtAnswer.Text = string.Empty;
            }
            else
            {
                if (original.Sign == -1)
                {
                    notValid = true;
                    sqrtErrorProvider.SetError(txtOriginalInt, "Please enter a positive integer");
                    txtSqrtAnswer.Text = string.Empty;
                }
            }
            return ! notValid;
        }

        /// <summary>
        /// Call the F# bigintSqrt function and display the result.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sqrtButton_Click(object sender, EventArgs e)
        {
            if (IsParsedAsPositiveInt())
            {
                // Call Sqrt
                original = BigInteger.Parse(txtOriginalInt.Text);
                answer = Roots.bigintSqrt(original);
                txtSqrtAnswer.Text = answer.ToString();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
My name is Rick Oden. I am a software developer living in Colorado. I have been developing code for various companies for more than 19 years. The languages I have used includes Pascal, Visual Basic, Delphi, Plex, C#, and now looking into F#.

Comments and Discussions