# Time Complexity: Primality

### Problem Statement :

```A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Given p integers, determine the primality of each integer and return Prime or Not prime on a new line.

Note: If possible, try to come up with an   primality algorithm, or see what sort of optimizations you can come up with for an O(n) algorithm. Be sure to check out the Editorial after submitting your code.

Function Description

Complete the primality function in the editor below.

primality has the following parameter(s):

int n: an integer to test for primality

Returns

string: Prime if n is prime, or Not prime
Input Format

The first line contains an integer, p, the number of integers to check for primality.
Each of the p subsequent lines contains an integer, n, the number to test.

Constraints

1  <=   p   <=  30
1   <=   n  <=  2 x 10^9```

### Solution :

```                            ```Solution in C :

In    C   :

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
int p;
int flag;
scanf("%d",&p);
for(int a0 = 0; a0 < p; a0++)
{
flag=0;
int n;
scanf("%d",&n);
if(n==1)
{
printf("Not prime\n");
flag=1;
break;
}
if(flag!=1){
for(int i=2;i<=sqrt(n);i++)
{

if(n%i==0 || (n%(n/i))==0)
{
printf("Not prime\n");
flag=1;
break;
}

}
}
if(flag==0)
{
printf("Prime\n");
}
}
return 0;
}```
```

```                        ```Solution in C++ :

In   C++  :

#include <iostream>
#include <math.h>
using namespace std;

int main(){
int soLanNhapVao, soNhapVao;
int soUoc = 1;
cin >> soLanNhapVao;
for(int i = 0;i < soLanNhapVao;i++){
cin >> soNhapVao;
for(int j = 2;j < sqrt(soNhapVao);j++){
if(soNhapVao%j == 0){
soUoc += 1;
}
else{
}
}
if(soNhapVao == 1){
cout << "Not prime" << endl;
}
else if(soUoc >= 2){
cout << "Not prime" << endl;
soUoc = 1;
}
else{
cout << "Prime" << endl;
soUoc = 1;
}
}
}```
```

```                        ```Solution in Java :

In  Java  :

import java.util.*;

public class Solution {

static boolean isPrime(int n) {
for(int i=2;i<=Math.sqrt(n);i++) {
if(n%i==0) {
return false;
}
}
return true;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numOfTests = sc.nextInt();
for (int i = 0; i < numOfTests; i++) {
int x = sc.nextInt();
String s;
if (x >= 2 && isPrime(x)) {
s = "Prime";
} else {
s = "Not prime";
}
System.out.println(s);
}
}
}```
```

```                        ```Solution in Python :

In   Python3  :

from math import *
p = int(input().strip())
for a0 in range(p):
n = int(input().strip())
f=5
if(n!=1):
for i in range(2,int(sqrt(n))):
if(n%i==0):
f=0
if(f==0 or n==1):
print("Not prime")
else:
print("Prime")```
```

## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

## Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

## Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

## Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

## Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

## Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a