ghostmkg / programming-language

You write code in any programming language you want. And push the code.
33 stars 79 forks source link

Create prime_number_generator.py #18

Closed 0mehedihasan closed 1 week ago

0mehedihasan commented 1 week ago

Explanation:

  1. is_prime(n):

This function checks if a given number n is prime.

A prime number is a number greater than 1 that has no divisors other than 1 and itself.

The function returns False for numbers less than or equal to 1.

It checks divisibility by looping through potential divisors from 2 up to the square root of n (because if n is divisible by a number larger than its square root, then it must also be divisible by a smaller factor).

  1. generate_primes(limit):

This function generates all prime numbers up to the specified limit.

It loops through each number from 2 to limit, checking if it's prime using the is_prime() function.

If a number is prime, it is added to the primes list.

The function returns the list of prime numbers.

  1. if name == "main"::

This is the main part of the script, which only runs if the script is executed directly.

It asks the user to input an upper limit and then uses the generate_primes() function to find and display all prime numbers up to that limit.

Example:

If the user inputs 10 as the upper limit, the output will be:

Prime numbers up to 10: [2, 3, 5, 7