Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / C#

Chinese Remainder Problem

Rate me:
Please Sign up or sign in to vote.
4.88/5 (11 votes)
17 Oct 2008CPOL3 min read 128.5K   592   14  
Solve the Chinese remainder problem cleverly
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ChineseRemainderProblem
{
    public partial class FormRemainderProblem : Form
    {
        public FormRemainderProblem()
        {
            InitializeComponent();
        }

        private void buttonSolve_Click(object sender, EventArgs e)
        {
            try
            {
                int a = int.Parse(textBoxA.Text);
                int b = int.Parse(textBoxB.Text);
                int c = int.Parse(textBoxC.Text);
                int r1 = int.Parse(textBox1.Text);
                int r2 = int.Parse(textBox2.Text);
                int r3 = int.Parse(textBox3.Text);

                int n0, t;
                RemainderProblem.Solve(a, b, c, r1, r2, r3, out n0, out t);

                textBoxN.Text = n0.ToString();
                textBoxT.Text = t.ToString();

                listBoxN.Items.Clear();
                for (int i = 0; i < 20; i++)
                {
                    listBoxN.Items.Add(n0 + t * i);
                }
                listBoxN.Items.Add("...");
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message, "No solution");
            }
        }
    }
}

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
Architect YunCheDa Hangzhou
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions