• code天天写代码重新出发


    //Prime Number Test
    public class Test{   
        public static void Main(){
            try{
                PrintPrime(100,400);
            }catch(System.Exception ex){
                System.Console.WriteLine(ex.Message);
            }
        }
        //print prime number between low and High
        static void PrintPrime(int low, int high){
            string strErrMsg = "";
            if(low < 0 || high< 0){       
                strErrMsg = "Natural number must be 1+ integer!";
                throw new System.ArgumentOutOfRangeException("",strErrMsg);
            }
            if(high > int.MaxValue || low < int.MinValue){           
                strErrMsg = "Integer Value is out of range !";
                throw new System.ArgumentOutOfRangeException("",strErrMsg);
            }
            int ix = low;
            for (;ix <= high; ix++){
                if(IsPrime(ix)){
                    System.Console.Write(ix + "\t");
                }
            }
        }
        // Is it a prime number     
        // returns : ture, is prime number
        //         : false,is not prime number       
        static bool IsPrime(int n){
            bool blnReturn = true;
            if (n <= 1){
                blnReturn = false;
            }
            for(int i=2; i<Sqrt(n); i++){
                if ( n % i == 0){
                    blnReturn = false;
                    break;
                }
            }
            return blnReturn;
        }
        //my Sqrt
        static double Sqrt(double num ){
            double E = 0.0000001f;
            double d = num /2;
            while (Abs(num - d * d) > E){
               d = (d + num / d) / 2;
            }
            return d;
        }    
        //my Abs
        static double Abs(double d){
            if ( d >= 0) {
                return d;
            }
            else{
                return -d;
            }
        }
    }

  • 相关阅读:
    学习WWDC的好资源!
    运行 CMD 时,參数加引號常见问题
    FileChannel的深入理解
    C#单例模式的三种写法
    Linux 安装Nginx具体图解教程
    计网面试题
    VS:&quot;64位调试操作花费的时间比预期要长&quot;的一解决途径
    中小型WEB系统权限日志数据表设计
    CDN服务上线,DNSPOD布局云端生态圈
    怎样利用ash监控会话
  • 原文地址:https://www.cnblogs.com/qinghao/p/1541216.html
Copyright © 2020-2023  润新知