#include <stdio.h>
#include <stdlib.h> // Judge i Is it prime , You need to determine i Can be (long)sqrt(i) Divide the number within
// if i Divisible by one of the prime numbers , be i Not prime ; otherwise i Prime number int main()
{
//n=10 ans=4
//n=100 ans=25
//n=1000 ans=168
//n=10000 ans=1229
//n=100000 ans=9592
//n=1000000 ans=78498
//n=10000000 ans=664579
//n=100000000 ans=5761455
long i,j,n,flag,ans=,t=;
long *zhi=(long *) malloc (sizeof(long)*);
scanf("%ld",&n);
zhi[]=;
for (i=;i<=n;i++)
{
// if "zhi[t]*zhi[t]==i" establish , be i Not prime , Don't continue to judge
// And greater than i You need to use zhi[t] Judge (j=0;j<t+1;j++)
// This method is better than " When the prime number is greater than (long)sqrt(i) Exit from time " Fast
// and (zhi[t]-1)*(zhi[t]-1)<=i<=n
// When i Less than longint The scope can be realized
if (zhi[t]*zhi[t]==i)
{
t++;
continue;
}
flag=;
for (j=;j<t;j++)
if (i%zhi[j]==)
{
flag=;
break;
}
if (flag)
{
zhi[ans]=i;
ans++;
}
}
printf("ans=%ld\n",ans);
/*
for (i=0;i<ans;i++)
printf("%ld ",zhi[i]);
printf("\n");
*/
/*
if (zhi[ans]==n)
printf("%ld is a prime\n",n);
else
printf("%ld is not a prime\n",n);
*/
return ;
}

seek n(n>=2) The prime number within / Judge whether a number is prime —— Method + More articles on detail optimization

  1. c# An algorithm to determine whether a number is prime or to find the common divisor of a number

    Is a number prime , It's about judging a number apart from 1 And whether it has any other divisors , If there is, it's a composite number , Otherwise it's prime . In fact, the essence is to seek the common divisor . What is the idea of finding the common divisor , It's just trying to find a smaller number , The divisor is its divisor , Otherwise keep trying , ...

  2. js How to judge whether a number is prime

    1. Let's first understand what prime numbers are ( namely : Can only be 1 The number divisible by itself is called prime number ) Main code /** * Judge whether the number is prime */ function isPrimeNum(num){ ; i < num/+; i++) ...

  3. c Judge the number of daffodils , Prime number ( prime number )

    #include<stdio.h> #include<stdbool.h> // Narcissistic number -- You know, the sum of cubes equals itself void sXh() { int x,y,z; printf(& ...

  4. pyhton Find all factors of a number as well as Judge whether a number is a prime number Two little scripts

    Recently I saw a website , The Euler plan . It's fun , It's all algorithmic problems . This is our website :http://projecteuler.net/problems  This is the Chinese station :http://pe.spiritzhang.com/ Next ...

  5. php To find the prime factor of a number

    php To find the prime factor of a number One . summary One sentence summary : Such a simple topic , Or write the position of variable definition and self increment wrong . 1 <?php 2 $num=trim(fgets(STDIN)); 3 // If $n ...

  6. C Language : seek n(n&lt;10000) All four leaf roses within .- The string s1 and s2 Merge to form a new string s3, Take out first 1 Put the first character of 3, Take it out again 2 Put the first character of 3,

    // function fun function : seek n(n<10000) Count all four leaf roses within and store them one by one in result In the index group , Number as the return value . If one 4 A bit integer is equal to the number of its bits 4 The sum of the powers , The number is called the function return value . #incl ...

  7. How to judge whether a number is prime (zt)

    How to judge whether a number is prime ? Stupid way of doing it : bool IsPrime(unsigned n){    if (n<2)    { // Less than 2 The number of is neither a composite number nor a prime number     throw 0;    ...

  8. [LeetCode] Prime Number of Set Bits in Binary Representation The number of nonzero bits in binary representation is prime

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  9. (Miller Rabin Algorithm ) Judge whether a number is prime

    1. Appointment x%y by x modulus y, namely x Divide y The remainder of the result , When x<y when ,x%y=x, All modulo operations are integers . x^y Express x Of y Power . The power operation takes precedence over multiplication, division and modulus , The priority of addition and subtraction is the lowest . see x^y/z this ...

Random recommendation

  1. [LeetCode] Combinations Combination

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  2. The leader / followers (Leader/Followers) Model and semi synchronization / Semi asynchronous (half-sync/half-async) Models are common customers - Server programming model

    The leader - followers (Leader/Followers) The metaphor of the model Half a synchronous / Semi asynchronous models and leaders / The difference between the follower model : Half a synchronous / The semi asynchronous model has an explicit queue of pending events , And leaders - The follower model doesn't have an explicit queue ( very ...

  3. ADB Tools obtain ROOT Permission and copy file method

    adb push d:\tm3_sqlit.db data/zouhao/tm3_sqlit.dbadb pull data/zouhao/tm3_sqlit.db d:\tm3_sqlit.db a ...

  4. 【Centos7】Delete virtual bridge

    Previously,Stop service which controls virtual bridges. sudo systemctl stop libvirtd.service #System ...

  5. (NO.00003)iOS Game simple robot projection game ( nineteen )

    If you read the children's shoes in the front blog, you may remember , We are Level1 In this paper, we write code to realize the movement of the basket . Writing code is not intuitive enough , You need to compile tests over and over again , Is there any other way ? The answer, of course, is : The big ones are ;) SpriteBuilder Baby has ...

  6. php Generate xml data

    1.php Generate xml The data are generally 2 Ways of planting , One is the assembly string , The other is to use php Built in system classes 2. Use php Built in class generation xml data 3. Assembly string generation xml data public function stat ...

  7. 18. Spring Boot 、 register Servlet Three components Servlet、Filter、Listener

    because SpringBoot The default is jar Package mode to start embedded Servlet Container to start SpringBoot Of web application , No, web.xml file public class MyServlet extends ...

  8. notepad Regular expressions Copy Text

  9. Use jquery operation session

    Abstract : What we're sharing today is using jquery To deal with it session. We will use sessionStorage object , It's similar to localStorage object , It's just sessionStorage It's for storage session data ...

  10. O(n log log n) Realization FGT and FLT(Fast GCD/LCM Transformation)

    In this paper, the author does not understand divide and rule FFT After that, I began to amuse myself When I see a problem, I ask for the correct solution , There's a strange transformation , It's very Transformation, I'm the same as the strange thing coming out of the solution , But I still want to write some ideas .. ...