lucproblem008.c (998B)
1 /* 2 * Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 12 Dec 2025 3 * Repo: https://github.com/notamitgamer/bsc 4 * License: MIT 5 */ 6 7 /* Write a problem to print all the prime numbers from 1 to 300. */ 8 /* Let Us C, Chap - 6, Page - 101, Problem 6.1 */ 9 10 // Method: Trial Division (Optimized to check up to sqrt(N)) 11 12 #include <stdio.h> 13 #include <math.h> 14 #include <stdbool.h> 15 16 #define LIMIT 300 17 18 int main() 19 { 20 printf("Prime numbers from 1 to 300 : 2"); // as 2 is the only even prime number 21 for (int i = 3; i <= LIMIT; i += 2) // skipping all other even number 22 { 23 int n = (int)sqrt(i); 24 bool prime = true; 25 26 for (int j = 3; j <= n; j += 2) 27 // an odd number is only devisable by another odd number. 28 // so, skipping even number. 29 { 30 if (i % j == 0) 31 { 32 prime = false; 33 break; 34 } 35 } 36 if (prime) 37 { 38 printf(" %d", i); 39 } 40 } 41 return 0; 42 }