1. 程式人生 > >1000以內完數

1000以內完數

完數:一個數字的所有約數除了本身相加,等於這個數字:例如

6:1+2+3=6;

28:1+2+4+7+14=28;

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 完數
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i =2; i < 1000; i++)
            {
                int l=0;
                for (int j = 1; j <=i-1; j++)
                {
                    if (i % j == 0 )
                    {
                        l = l + j;
                    }
                }
                if (l==i)
                {
                    label2.Text =label2.Text+ l.ToString() + ",";
                }
            }
        }
    }
}