-
Gives a function that determines whether a number is prime or not.
bool judegenum(int num)if(num == 1)
return true;
for(int i=2; ireturn false;
return true;
The main function is given.
int main()
for(int i=1; i<=100;i++)if(judgenum(i) == true).
printf("%d", i);
-
I haven't learned c, but I wrote one in c++, you refer to the idea (only write the key parts) for(i=0; i<100;i++)
for(j=2;jif(i%j==0)
flag=1;If a factor is found, let flag=1
-
The first 2 c, mine is c
public static void main()bool isv = new byte[101];
for(int i = 0;i < 101; i++)isv[i] = true;
for(int i = 3; i < 101;i++)if(isv[i]) for(int j = 2; i * j <= 100; j++)isv[i * j] = false;
for(int i = 0;i < 101; i++)"", isv[i]);
-
If you want to use c to calculate the sum of all prime numbers between 1 and 100, you must first know what a prime number is?
Whether a number is prime or notA "prime number" is a natural number that cannot be divisible by any integer except 1 and itself. Like 1, 2, 3, 5, 7 are all prime numbers.
Once you understand the concept of prime numbers, then the program is easy to write. To know if a number x is prime, then all the integers between 1 x need to be divided by x, and if there is such a number (except 1 and x) that is divisible by x (with a remainder of 0), then x is not prime, otherwise x is prime. This is a general solution.
Then it's up to you to optimize it.
Actually, we don't need to check all integers between 1 x. Just check the number between 1 [root number x] and you're good to go. As for why, it can be understood from the perspective of multiplication, we are doing division, in fact, the reverse is multiplication.
Let's take the case of determining whether 15 is prime or not. When we divide by 3, 3*5=15. So, while judging 3, we also judge another number 5.
So we just need to check the root number x, and when the root number x is checked, the root number x will also be checked after the root number x.
**(c#private static void printprimes(int n)
if (j > k)}}
printprimes(100);All prime numbers from 1 to 100 can be printed.
Note the for loop.
#include
#include >>>More
#include
using namespace std; >>>More
Consider putting these 100 numbers in an array first, taking a random position one at a time (1-100 for the first time, 1-99 for the second time,..).Swapping the number of that position with the number of the last position will do the trick. >>>More
bool f=0;
for(int i=1000;i>=1;i--) Start with the largest number of 1000 and try to the smaller number. >>>More
The base and high area of the simple input triangle are presented as follows: >>>More