For Python, obtain 0 ~ All prime numbers within 100, python prime numbers
Prime number. A natural number greater than 1, except 1 and itself, cannot be divisible by other natural numbers called prime numbers; otherwise, it is called a union number.
1. Determine whether a number is a prime number:
Definition-based
Def is_prime (num): if num <= 1: return '% d is a combination of' % num for I in range (2, num): if not num % I: return '% d is a sum' % num else: return '% d is a prime number' % num
Considering the nature of the sum
Def is_prime (num): if num <= 1: return '% d is a combination of' % num for I in range (2, int (num/2) + 1 ): if not num % I: return '% d is a combination number' % num else: return '% d is a prime number' % num
2. Obtain 0 ~ Prime number less than 100
Def all_prime (num): lst = [] if num <= 1: return '0 ~ No prime number within % d '% num for I in range (2, num + 1): for j in range (2, int (I/2) + 1 ): if not I % j: break else: lst. append (I) return lst
Ps: Let's look at the small C language to find the prime number within 1 -.
The Code is as follows:
#include <stdio.h>#include <math.h>void main(){for (int i = 1; i <= 100; ++i){int j = 2;for (; j<=sqrt(i); ++j){if (i%j == 0)break;}if (j>sqrt(i) && i != 1)printf("%d ", i);}system("pause");}
Summary
The above is a small series of Python for everyone to find 0 ~ All prime numbers of less than 100 are expected to be helpful to everyone. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!