Numbers with exactly 3 divisors | C # program to print all three - primes smaller than or equal to n using Sieve of Eratosthenes ; Generates all primes upto n and prints their squares ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; print squares of primes upto n ; Driver program
class GFG { static void numbersWith3Divisors ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; prime [ 0 ] = prime [ 1 ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == false ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = true ; } } System . Console . WriteLine ( " Numbers ▁ with ▁ 3 ▁ divisors ▁ : ▁ " ) ; for ( int i = 0 ; i * i <= n ; i ++ ) if ( ! prime [ i ] ) System . Console . Write ( i * i + " ▁ " ) ; } public static void Main ( ) { int n = 96 ; numbersWith3Divisors ( n ) ; } }