zoukankan      html  css  js  c++  java
  • Java学习-素数

    求1000 0000以内素数个数?

    素数定义:除了1和它本身以外的不能整除它

     1 import charactor.ADHero;
     2 public class test
     3 {
     4     public static void main(String[] args) {
     5     long startTime = System.currentTimeMillis();
     6     int res=0;
     7     for(int n=1;n<10000000;n++)
     8     {
     9         int flag=0;
    10         for(int i=2;i<=Math.sqrt(n);i++)
    11         {
    12             if(n%i==0){
    13                 flag=1;
    14                 break;
    15             }
    16         }
    17         if(flag==0)
    18             res++;
    19     }
    20     long endTime=System.currentTimeMillis();
    21     System.out.println("1000 0000以内的质数共有:"+res+"个");
    22     System.out.println("共耗时:"+(endTime-startTime)/1000+"秒");
    23 }
    24 }

    运行结果

  • 相关阅读:
    leetcode 414
    Leetcode 495
    Leetcode 485题
    Python 24点(2)
    python 24点
    我的第一次作业
    Django
    multiprocessing模块
    遍历文档树
    shutil模块
  • 原文地址:https://www.cnblogs.com/gilgamesh-hjb/p/12161501.html
Copyright © 2011-2022 走看看