• 【练习】哥德巴赫猜想验证程序


    一个练习的程序,分为两部分。左边列表显示算出的素数,右边显示每个偶数的两个奇素数之和。

    程序很简陋,还请大家多提意见。

    Form1.Designer.cs

      1 namespace Gedebahe
      2 {
      3     partial class Form1
      4     {
      5         /// <summary>
      6         /// 必需的设计器变量。
      7         /// </summary>
      8         private System.ComponentModel.IContainer components = null;
      9 
     10         /// <summary>
     11         /// 清理所有正在使用的资源。
     12         /// </summary>
     13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
     14         protected override void Dispose(bool disposing)
     15         {
     16             if (disposing && (components != null))
     17             {
     18                 components.Dispose();
     19             }
     20             base.Dispose(disposing);
     21         }
     22 
     23         #region Windows 窗体设计器生成的代码
     24 
     25         /// <summary>
     26         /// 设计器支持所需的方法 - 不要
     27         /// 使用代码编辑器修改此方法的内容。
     28         /// </summary>
     29         private void InitializeComponent()
     30         {
     31             this.btnStart = new System.Windows.Forms.Button();
     32             this.label1 = new System.Windows.Forms.Label();
     33             this.btnStop = new System.Windows.Forms.Button();
     34             this.lPn = new System.Windows.Forms.ListBox();
     35             this.txtNumber = new System.Windows.Forms.TextBox();
     36             this.btnSuspend = new System.Windows.Forms.Button();
     37             this.label2 = new System.Windows.Forms.Label();
     38             this.txtEndNumber = new System.Windows.Forms.TextBox();
     39             this.leven = new System.Windows.Forms.ListBox();
     40             this.SuspendLayout();
     41             // 
     42             // btnStart
     43             // 
     44             this.btnStart.Location = new System.Drawing.Point(3018);
     45             this.btnStart.Name = "btnStart";
     46             this.btnStart.Size = new System.Drawing.Size(7523);
     47             this.btnStart.TabIndex = 0;
     48             this.btnStart.Text = "Start";
     49             this.btnStart.UseVisualStyleBackColor = true;
     50             this.btnStart.Click += new System.EventHandler(this.button1_Click);
     51             // 
     52             // label1
     53             // 
     54             this.label1.AutoSize = true;
     55             this.label1.Location = new System.Drawing.Point(1013);
     56             this.label1.Name = "label1";
     57             this.label1.Size = new System.Drawing.Size(7712);
     58             this.label1.TabIndex = 1;
     59             this.label1.Text = "Start Number";
     60             // 
     61             // btnStop
     62             // 
     63             this.btnStop.Location = new System.Drawing.Point(4878);
     64             this.btnStop.Name = "btnStop";
     65             this.btnStop.Size = new System.Drawing.Size(7523);
     66             this.btnStop.TabIndex = 4;
     67             this.btnStop.Text = "Stop";
     68             this.btnStop.UseVisualStyleBackColor = true;
     69             this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
     70             // 
     71             // lPn
     72             // 
     73             this.lPn.FormattingEnabled = true;
     74             this.lPn.ItemHeight = 12;
     75             this.lPn.Location = new System.Drawing.Point(1350);
     76             this.lPn.Name = "lPn";
     77             this.lPn.Size = new System.Drawing.Size(114316);
     78             this.lPn.TabIndex = 5;
     79             // 
     80             // txtNumber
     81             // 
     82             this.txtNumber.Location = new System.Drawing.Point(948);
     83             this.txtNumber.Name = "txtNumber";
     84             this.txtNumber.Size = new System.Drawing.Size(5521);
     85             this.txtNumber.TabIndex = 6;
     86             this.txtNumber.Text = "3";
     87             // 
     88             // btnSuspend
     89             // 
     90             this.btnSuspend.Location = new System.Drawing.Point(3918);
     91             this.btnSuspend.Name = "btnSuspend";
     92             this.btnSuspend.Size = new System.Drawing.Size(7523);
     93             this.btnSuspend.TabIndex = 7;
     94             this.btnSuspend.Text = "Suspend";
     95             this.btnSuspend.UseVisualStyleBackColor = true;
     96             this.btnSuspend.Click += new System.EventHandler(this.btnSuspend_Click);
     97             // 
     98             // label2
     99             // 
    100             this.label2.AutoSize = true;
    101             this.label2.Location = new System.Drawing.Point(15513);
    102             this.label2.Name = "label2";
    103             this.label2.Size = new System.Drawing.Size(6512);
    104             this.label2.TabIndex = 8;
    105             this.label2.Text = "End Number";
    106             // 
    107             // txtEndNumber
    108             // 
    109             this.txtEndNumber.Location = new System.Drawing.Point(2268);
    110             this.txtEndNumber.Name = "txtEndNumber";
    111             this.txtEndNumber.Size = new System.Drawing.Size(6821);
    112             this.txtEndNumber.TabIndex = 9;
    113             this.txtEndNumber.Text = "1000000";
    114             // 
    115             // leven
    116             // 
    117             this.leven.FormattingEnabled = true;
    118             this.leven.ItemHeight = 12;
    119             this.leven.Location = new System.Drawing.Point(17950);
    120             this.leven.Name = "leven";
    121             this.leven.Size = new System.Drawing.Size(374316);
    122             this.leven.TabIndex = 10;
    123             // 
    124             // Form1
    125             // 
    126             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    127             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    128             this.ClientSize = new System.Drawing.Size(574377);
    129             this.Controls.Add(this.leven);
    130             this.Controls.Add(this.txtEndNumber);
    131             this.Controls.Add(this.label2);
    132             this.Controls.Add(this.btnSuspend);
    133             this.Controls.Add(this.txtNumber);
    134             this.Controls.Add(this.lPn);
    135             this.Controls.Add(this.btnStop);
    136             this.Controls.Add(this.label1);
    137             this.Controls.Add(this.btnStart);
    138             this.Name = "Form1";
    139             this.Text = "Form1";
    140             this.Load += new System.EventHandler(this.Form1_Load);
    141             this.ResumeLayout(false);
    142             this.PerformLayout();
    143 
    144         }
    145 
    146         #endregion
    147 
    148         private System.Windows.Forms.Button btnStart;
    149         private System.Windows.Forms.Label label1;
    150         private System.Windows.Forms.Button btnStop;
    151         private System.Windows.Forms.ListBox lPn;
    152         private System.Windows.Forms.TextBox txtNumber;
    153         private System.Windows.Forms.Button btnSuspend;
    154         private System.Windows.Forms.Label label2;
    155         private System.Windows.Forms.TextBox txtEndNumber;
    156         private System.Windows.Forms.ListBox leven;
    157     }
    158 }

    Form1.cs

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Windows.Forms;
      4 using System.Threading;
      5 
      6 namespace Gedebahe
      7 {
      8     public partial class Form1 : Form
      9     {
     10         Thread t1;
     11         public Form1()
     12         {
     13             InitializeComponent();
     14             Form1.CheckForIllegalCrossThreadCalls = false;
     15         }
     16 
     17         private void Form1_Load(object sender, EventArgs e)
     18         {
     19 
     20         }
     21 
     22         private void button1_Click(object sender, EventArgs e)
     23         {
     24             lPn.Items.Clear();
     25             lPn.Items.Add(1);
     26             pn = new List<int>();
     27             pn.Clear();
     28             pn.Add(1);
     29             t1 = new Thread(CaculatePn);
     30             t1.Start();
     31         }
     32 
     33 
     34         private void CaculatePn()
     35         {
     36             for (int i = int.Parse(txtNumber.Text); i < int.Parse(txtEndNumber.Text); i++)
     37             {
     38                 if (i%2 == 0)
     39                 {
     40                     foreach (int j in pn)
     41                     {
     42                         if (pn.Contains(i - j))
     43                         {
     44                             leven.Items.Insert(0,i + "=" + j + "+" + (i - j));
     45                             break;
     46                         }
     47 
     48                     }
     49                 }
     50                 else
     51                 {
     52                     if (isPn(i))
     53                     {
     54                         pn.Add(i);
     55                         lPn.Items.Insert(0, i);
     56                     }
     57 
     58                 }
     59             }
     60         }
     61 
     62         private List<int> pn;
     63 
     64         private bool isPn(int _num)
     65         {
     66             bool _isPn = true;
     67             if (_num == 1 || _num == 3||_num==5||_num==7return true;
     68             if (_num == 2 || _num == 4return false;
     69             int _sqrt = (int) Math.Ceiling(Math.Sqrt((double) _num));
     70             foreach (int i in pn)
     71             {
     72                 if(i==1continue;
     73                 if (i < _sqrt)
     74                 {
     75                     if (_num%== 0)
     76                     {
     77                         _isPn = false;
     78                         break;
     79                     }
     80                 }else break;
     81             }
     82             return _isPn;
     83         }
     84 
     85         private void btnStop_Click(object sender, EventArgs e)
     86         {
     87             t1.Abort();
     88         }
     89 
     90         private void btnSuspend_Click(object sender, EventArgs e)
     91         {
     92             if(t1.ThreadState == ThreadState.Running)
     93                 t1.Suspend();
     94             else
     95             {
     96                 t1.Resume();
     97             }
     98         }
     99     }
    100 }

    运行效果图

    作者:thanks       微信:-       QQ:305380844
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    基于CentOS7配置ArcGIS enterprise
    ArcGIS pro 发布地图服务(一)动态地图服务
    ArcGIS操作技巧——怎样把地图放到PPT中,并且进行编辑?
    ArcGIS Earth1.9最新版安装和使用教程
    ArcGIS pro2.3中添加天地图底图
    excle函数
    网闸和防火墙
    NoSQL——not onlySQL不仅仅是SQL
    leaflet学习一 入门
    openlayer3 基础学习一创建&显示地图
  • 原文地址:https://www.cnblogs.com/thanks/p/1833135.html
Copyright © 2020-2023  润新知