A python program to find prime numbers! (algorithm code How to) (blog python)

Python & prime numbers

Hey guys, Today I am gonna be blogging about something more simple than I usually do! Today I am gonna be showing you guys how to make a python program to list out prime numbers. It will be a cool little python project for you guys and I challenge you to do this on your own first because it is pretty fun and can really get you thinking!

The code explanation!

This program won't be too hard and again I challenge you to do this on your own first! but anyway let's get into how this is gonna work. To start off we will have a list to store all our numbers. Then we need to get into the algorithm. First off we use a for loop to run through all numbers (1-100). Now we know that prime numbers are only divisible by 1 and themselves. So with this info, we can add another for loop inside the last loop to loop through the numbers 1 to 10 wow lots of loops! And we check if our number we get from the first loop is dividable by any number besides itself and 1. So we can simply add all numbers that are only divisible by 1 and themselves and bam! We got all the primes. So with that let's get into the code! don't worry if that did not make much sense ill walk you guys through it with the code

The code how to!

Ok so let's get started with the first line of code! 
first off just add this to your script. It's just a list variable to store our primes. 

Cool! Now, let's add the two for loops. The first one is to loop through all our numbers and the second is to check if the number is divisible by anything else besides 1 and itself.
Ok now that we are in the loop we need to check if our number is divisible by the other number we are on (J/I) we can do this easily with a variable
Now that we have the number we need to check if the number we are dividing by(I) is 1 or itself. Because if it is we don't care because we just need to find out if it's divisible by anything besides those two. We can do this with an if statement so just adds this to the script! BUT! we don't want to check if it is 1 or itself we want to check if it's not 1 or itself. Because it's always better to check if it's not that if it is! (IDK why it just is)
add this to the code
You might have noticed that bit of code at the end. It's just to check if the number is a whole number. Because if it is we move on because this number can be divided by a number beside itself and 1 so we know it's not a prime number!
Ok, let's add the last bit of logic! so here is how we are gonna go about this! I'll show you the code then explain. So add this code to the script!

So the code "IF I == 9:" checks if we are at the end of the loop. Because how are we sure this number is not divisible by anything besides 1 and itself if we have not checked every number up to 9 that's why we have a 9 there. So this code will only execute if (I) is 9 so we ran through the loop 9 times.

After that with the code "IF J NOT IN NUMS" we check if the number we are adding (J) is not already on the list. Then after that we just append it in the (nums) list with the other primes and BAM! we are done! yay!

Final thoughts

Thanks for stopping by guys. If you enjoyed you could follow share or leave a comment thanks!
this was lots of fun to make and I appreciate anyone who helps out in my first few posts. thank you all! if you have any thoughts or questions id be happy to answer them. And with that keep on creating and exploring and ill be heading off for now!

Git and socials





thank you.

Comments

  1. Hey guys hope you enjoyed this one!

    Make sure to subscribe to the blog if you want to see more and leave a comment to boost me up! If you don't know what to comment just say "great blog!"

    ReplyDelete
  2. This was a fun little challenge to create! good job

    ReplyDelete
    Replies
    1. im glad to hear you liked it! thanks for reading

      Delete
  3. I should probably do more things like this to help out my python skills

    thanks for sharing keep it up man!

    ReplyDelete
    Replies
    1. Yea it always a good idea to follow along!

      and np! i plan to keep it coming!

      Delete

Post a Comment

Popular posts from this blog

5 amazing sites where you can build your coding knowledge!