3 Divisors Python

PROGRAM TO PRINT NUMBERS WITH EXACTLY THREE DIVISORS


def numbersWith3Divisors(n): 
   
    prime=[True]*(n+1); 
    prime[0] = prime[1] = False;
    p=2;
    while (p*p<=n):
        # If prime[p] is not changed, then it is a prime 
        if (prime[p] == True):
            # Update all multiples of p
            for i in range(p*2,n+1,p):
                prime[i] = False
        p+=1
  
    # print squares of primes upto n. 
    print("Numbers with 3 divisors :");
    i=0;
    while (i*i <= n): 
        if (prime[i]):
            print(i*i,end=" ");
        i+=1;
  
# driver program 
  
n = 96
numbersWith3Divisors(n); 


OUTPUT

Numbers with 3 divisors :
4 9 25 49 

CREDITS: https://www.geeksforgeeks.org/numbers-exactly-3-divisors/

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java