1.
Sieve Algorithm :
• Time Complexity : The dominant term in the .me complexity is O(n log log n), which makes
this the overall .me complexity of the algorithm. The Sieve of Eratosthenes is very efficient
for genera.ng all primes up to a large number.
• Space Complexity : The space complexity of the Sieve of Eratosthenes algorithm is O(n),
where n is the upper limit up to which we want to find prime numbers.
Program
public class Main {
public sta.c void simpleSieve(int limit) {
boolean[] prime = new boolean[limit + 1];
for (int i = 2; i <= limit; i++) {
prime[i] = true;
// Mark all the mul.ples of the prime numbers
for (int p = 2; p * p <= limit; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= limit; i += p) {
prime[i] = false;
// Print all prime numbers
for (int p = 2; p <= limit; p++) {
if (prime[p] == true) {
System.out.print(p + " ");
public sta.c void main(String[] args) {
simpleSieve(50);
} }
2. Segmented Sieve Algorithm
Time Complexity : O((h - l + 1) log log h) or O(n log logn)
Space Complexity : The space complexity is O(h - l + 1) for the boolean array prime that
stores whether each number in the range
Program :
import java.u.l.*;
public class Main {
sta.c void SegSieve(int l, int h) {
boolean prime[] = new boolean[h - l + 1];
Arrays.fill(prime, true);
if (l == 1)
l = 2;
for (int p = 2; p * p <= h; p++) {
int sm = Math.max(p * p, (l + p - 1) / p * p);
for (int i = sm; i <= h; i +=p) {
prime[i - l] = false;
}
}
for (int i = l; i <= h; i++) {
if (prime[i - l]) {
System.out.print(i + " ");
}
}
}
public sta.c void main(String[] args) {
SegSieve(10, 30);
}
}
3. Euler Phi Algorithm :
Time Complexity : O(sqrt(n))
Space Complexity : O(1)
Program :
import java.u.l.*;
public class Main {
// Returns the value of Euler's to.ent func.on phi(n)
public sta.c int phi(int n) {
int result = n; // Ini.alize result as n
// Check for all prime factors of n and subtract their mul.ples from result
for (int p = 2; p * p <= n; p++) {
if (n % p == 0) { // p is a prime factor of n
while (n % p == 0) { // Remove all mul.ples of p from n
n /= p;
result -= result / p;
// If n has a prime factor greater than sqrt(n), then add its contribu.on
if (n > 1) {
result -= result / n;
return result;
// Main method to test the program
public sta.c void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int phi_n = phi(n);
System.out.println("phi(" + n + ") = " + phi_n);
sc.close(); }}
4. StrobogrammaJc number :
Time Complexity : O(n)
Space Complexity : O(1)
Program :
import java.u.l.*;
class Main {
public sta.c boolean isStrobogramma.c(String num) {
Map<Character, Character> map = new HashMap<Character, Character>();
map.put('6', '9');
map.put('9', '6');
map.put('0', '0');
map.put('1', '1');
map.put('8', '8');
int l = 0, r = num.length() - 1;
while (l <= r) {
if (!map.containsKey(num.charAt(l))) return false;
if (map.get(num.charAt(l)) != num.charAt(r))
return false;
l++;
r--;
return true;
public sta.c void main(String[] args) {
String n= "8698";
boolean result = isStrobogramma.c(n);
System.out.println(result); } }
5. Chinese remainder Theorem
Time Complexity: O(k * P), where k is the number of modular conditions and P is the
product of all the elements in the num[] array.
Space Complexity : O(k)
Program :
import java.u.l.*;
class Main {
int calculate(int k , int num[], int rem[])
int j , x = 1;
while(true)
for( j=0;j<k;j++)
if(x%num[j] != rem[j] )
break;
if(j ==k)
return x;
x++;} }
public sta.c void main (String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter Divisor");
int k= sc.nextInt();
int [] num = new int[k];
for(int i=0; i<k; i++)
num[i] = sc.nextInt();
System.out.println("Enter Remainder");
int rem[]=new int[k];
for(int i=0; i<k; i++)
rem[i] = sc.nextInt();
Main c = new Main();
System.out.println( c.calculate(k, num, rem));
} }
6. Toggle the Switch :
Time Complexity : O(n log n)
Space Complexity : O(n)
Program :
import java.u.l.Scanner;
class Main{
public sta.c void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
boolean b[] = new boolean[n+1];
int i,j,c=0;
for(i=1;i<=n;i++)
for(j=i;j<=n;j+=i)
if(b[j]==false)
b[j] = true;
else
b[j] = false;
}}
for(i=1;i<=n;i++)
{
if(b[i]==true)
c++;
System.out.println(c);
7. Alice Apple Tree
Time Complexity : O(cubicroot(n)) O(3ⱴn)
Space Complexity : O(1)
Program :
import java.u.l.*;
public class Main{
public sta.c void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int cnt=0,sum=0;
while(sum<n){
cnt++;
sum+=(12*cnt*cnt);
System.out.println((8*(cnt)));