Python Prime Number Checker Challenge

Hello connections 👋 Welcome to Day 2 of my Python problem-solving series! 🧠 Day 2 Challenge: Check if a number is Prime A prime number is a number greater than 1 that has only two factors: 1 and itself. 👉 Example: Input: 7 → Output: Prime Input: 9 → Output: Not Prime My Approach 1: Basic (Factor Counting Method) num = int(input("Enter a number: ")) c = 0 if num <= 1: print("Not prime") else: for i in range(1, num + 1): if num % i == 0: c += 1 if c == 2: print("Prime") else: print("Not prime") 📌 Explanation: Here, we count how many numbers divide num. If the count is exactly 2 → it’s a prime number. ⚡ My Approach 2: Optimized num = int(input("Enter a number: ")) if num <= 1: print("Not prime") else: for i in range(2, int(num**0.5) + 1): if num % i == 0: print("Not prime") break else: print("Prime") 📌 Explanation: Instead of checking all numbers, we only check up to √n, which makes the solution much faster for large inputs. Now it’s your turn 👇 Try solving it using your own approach or suggest improvements! Let’s learn and grow together 🚀 #Python #CodingChallenge #ProblemSolving #30DaysOfCode #Programming

To view or add a comment, sign in

Explore content categories