For Python, obtain 0 ~ All prime numbers within 100, python prime numbers

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.