zoukankan      html  css  js  c++  java
  • 编程实践66

    编程实践6-6

    课程元素类型 任务

    歌德巴赫猜想:任何一个大于等于6的偶数可以分解为两个素数的和,通过程序来实现对哥德巴赫猜想的验证。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Prime_number
    {
        class Program
        {
            static void Main(string[] args)
            {
                int nA,nB;
                //输入部分
                nA = Convert.ToInt32(Console.ReadLine());
    
                nB = ChaiFen(nA);
                //判断是否为素数
                if (nB == 0)
                {
                    Console.WriteLine("您输入的数 {0} 无法拆分成两个素数", nA);
                }
                else
                {
                    Console.WriteLine("您输入的数 {0} 可以分成两个素数,为{1}  {2}", nA,nB,nA - nB);
                }
    
                Console.ReadKey();
    
            }
            public static bool prime(int iA)
            {
                for (int i = 2; i <= Math.Sqrt(iA); i++)
                {
                    if (iA % i == 0)
                    {
                        return false;
                    }
                }
                //到这里说明不是素数 返回true
                return true;
    
            }
            public static int ChaiFen(int iA)
            {
    
                int i;
                bool bA, bB;
    
                for (i = 3; i <= Math.Sqrt(iA); i = i + 2)
                {
                    bA = prime(i);
                    if (bA)
                    {
                        bB = prime(iA - i);
                        if (bA)
                        {
                            return i;
                        }
                    }
                }
                return 0;
    
            }
        }
    }
  • 相关阅读:
    中芯国际唐镇生活园区二期奠基 助力员工安居乐业
    权限管理架构
    不登录电脑启动程序
    Nagios 系统监控
    JieBaNet+Lucene.Net
    FontAwesome 图标
    Net多线程编程
    Scala Control Structures
    OAuthLogin2.0
    Telnet服务器和客户端请求处理
  • 原文地址:https://www.cnblogs.com/Wzqa/p/2813997.html
Copyright © 2011-2022 走看看