Results 1 to 1 of 1

Math Help - Co-Prime Finder

  1. #1
    Bar0n janvdl's Avatar
    Joined
    Apr 2007
    From
    South Africa
    Posts
    1,630
    Thanks
    6

    Co-Prime Finder

    Finally got it to work!

    Source code:
    Code:
    public partial class frmPrimes : Form
        {
            ArrayList iNumberArray = new ArrayList();
            int iNumber;
    
            public frmPrimes()
            {
                InitializeComponent();
            }
    
            private void btnFind_Click(object sender, EventArgs e)
            {         
                iNumberArray.Clear();
    
                lstPrimes.Items.Clear();
    
    
                iNumber = Convert.ToInt32(txtNumber.Text);
    
                for (int k = 1; k <= iNumber; k++)
                {
                    if ((iNumber % k) == 0)
                    {
                        iNumberArray.Add(k);
                    }                
                }
    
                prgPrimes.Maximum = iNumber;
    
                RunThrough(iNumber);
    
                if (chkSum.Checked == true)
                {
                    SumPrimes();
                }            
            }
    
            private void RunThrough(int iNumber)
            {
                int iPrimes = 0;
    
                for (int i = 1; i <= iNumber; i++)
                {
                    for (int j = 1; j <= i; j++)
                    {
                        if (((i % j) != 0))
                        {
                            
                        }
                        else if ((iNumberArray.IndexOf(j) >= 0))
                        {
                            iPrimes++;
                        }
                    }
                    if (iPrimes < 2)
                    {
                        lstPrimes.Items.Add(i.ToString());
                        iPrimes = 0;
                    }
                    else
                    {
                        iPrimes = 0;
                    }
    
                    prgPrimes.Value = i;
                    Application.DoEvents();
                }
            }
    
            private void SumPrimes()
            {
                int iTotal = 0;
                for (int i = 0; i <= lstPrimes.Items.Count - 1; i++)
                {
                    iTotal += Convert.ToInt32(lstPrimes.Items[i].ToString());
                }
    
                MessageBox.Show("Total: " + iTotal.ToString() + "  ---  Number of Primes: " + lstPrimes.Items.Count.ToString() );
            }
        }

    My 25th post!
    Attached Files Attached Files
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 1
    Last Post: October 22nd 2011, 12:37 PM
  2. Pattern finder
    Posted in the Math Software Forum
    Replies: 4
    Last Post: May 24th 2011, 05:43 AM
  3. sequence finder
    Posted in the Algebra Forum
    Replies: 1
    Last Post: October 17th 2009, 01:54 PM
  4. diagonal finder
    Posted in the Geometry Forum
    Replies: 4
    Last Post: April 30th 2008, 11:49 PM

Search Tags


/mathhelpforum @mathhelpforum