Skip to content

Commit cbb6dda

Browse files
authored
Merge pull request #1 from Mukulbaid63/new-1
New 1
2 parents a7b7e83 + 255dde0 commit cbb6dda

File tree

10 files changed

+373
-0
lines changed

10 files changed

+373
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Java program to demonstrate working of extended
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class Euclid
6+
{
7+
/*Recursive function to calculate gcd using Euclid's algo*/
8+
public static int gcd(int a,int b)
9+
{
10+
if (a==0)
11+
return b;
12+
13+
return gcd(b%a, a);
14+
}
15+
16+
public static void main(String[] args)
17+
{
18+
Scanner sc=new Scanner(System.in);
19+
20+
/*Taking a & b as user input*/
21+
int a=sc.nextInt();
22+
int b=sc.nextInt();
23+
24+
System.out.println("GCD : " + gcd(a,b)); //Output
25+
26+
}
27+
28+
}
29+

Java/Euclid's GCD Algorithm/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
# Sample Input:
3+
4+
10 15
5+
6+
7+
8+
# Sample Output:
9+
10+
5
11+
12+
13+
14+
# Sample Input:
15+
16+
20 48
17+
18+
19+
20+
# Sample Output:
21+
22+
4
23+
24+
25+
26+
# Explaination:
27+
TC 1:
28+
10 = 2 * 5
29+
30+
15 = 3 * 5
31+
32+
The Greatest common divisor is 5
33+
34+
35+
36+
TC 2:
37+
20 = 2 * 2 * 5
38+
39+
48 = 2 * 2 * 2 * 2 * 3
40+
41+
The Greatest common divisor is 2 * 2 = 4
42+
43+
44+
45+
The algorithm is based on below facts:
46+
47+
48+
49+
--If we subtract smaller number from larger (we reduce larger number), GCD doesn’t change. So if we keep subtracting repeatedly the larger of two, we end up with GCD.
50+
51+
--Now instead of subtraction, if we divide smaller number, the algorithm stops when we find remainder 0.
52+
53+
54+
55+
# Time Complexity:
56+
57+
<h3>O(log min(a,b))</h3>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Relevance in Cryptography:
2+
RSA, which based on the great difficulty of integer factorization, is the most widely-used public-key cryptosystem used in electronic commerce. Euclid algorithm and extended Euclid algorithm are the best algorithms to solve the public key and private key in RSA. Extended Euclid algorithm in IEEE P1363 is improved by eliminating the negative integer operation, which reduces the computing resources occupied by RSA, hence has an important application value.
3+
4+
5+

Java/FloydAlgorithm/FloydAlgo.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.lang.*;
4+
//Node of Linked list
5+
class Node
6+
{
7+
int data; //value of the node
8+
Node next; //next node
9+
/*Constructor*/
10+
Node(int x)
11+
{
12+
data = x;
13+
next = null;
14+
}
15+
}
16+
public class DetectLoop
17+
{ //function to create loop in linked list
18+
public static void createLoop(Node head, Node tail, int k){
19+
//base case
20+
if (x == 0) return;
21+
Node curr = head;
22+
23+
for(int i=1; i<k; i++)
24+
curr = curr.next;// traversing the linked list till k
25+
tail.next = curr;
26+
}
27+
// Driver Code
28+
public static void main (String[] args){
29+
30+
Scanner sc = new Scanner(System.in);
31+
32+
//no. of nodes in linked list
33+
int n = sc.nextInt();
34+
35+
//head of Linked list
36+
int num = sc.nextInt();
37+
Node head=new Node(num);
38+
39+
Node tail=head;
40+
41+
//Linked List created
42+
for(int i=0; i<n-1; i++)
43+
{
44+
num = sc.nextInt();
45+
tail.next = new Node(num);
46+
tail = tail.next;
47+
}
48+
49+
//Node to check if loop exists or not
50+
int temp = sc.nextInt();
51+
52+
//creating the loop
53+
createLoop(head,tail,temp);
54+
55+
//creating object of x
56+
Solution x = new Solution();
57+
58+
//if loop exists
59+
if(x.detectLoop(head))
60+
System.out.println("Loop exists");
61+
else
62+
System.out.println("Loop doesn't exists");
63+
}
64+
}
65+
66+
//detecting if the loop exists or not
67+
class Solution {
68+
69+
public static boolean detectLoop(Node head){
70+
//base case
71+
if(head==null||head.next==null)
72+
return false;
73+
74+
Node slow = head;
75+
Node fast = head;
76+
77+
while(fast != null && fast.next != null) {
78+
79+
slow = slow.next;
80+
fast = fast.next.next;
81+
82+
//if loop exists slow and fast pointer will surely intersect
83+
if(slow==fast)
84+
return true;
85+
}
86+
87+
return false;
88+
}
89+
}

Java/FloydAlgorithm/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
# Sample Input:
3+
4+
5
5+
6+
1 6 3 7 8
7+
8+
2
9+
10+
11+
12+
# Sample Output:
13+
14+
Loop exists
15+
16+
17+
18+
# Explaination:
19+
20+
4--> size of linked list
21+
22+
1-->6-->3-->7-->8 =>nodes of linked list
23+
24+
2---> node to be checked
25+
26+
</br>
27+
28+
after creating the loop the detect loop fuction is called and the two pointer slow and fast meets if loop exists .
29+
For better understanding,refer <a href="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190621160855/Detect-loop-in-a-linked-list.png">here:
30+
31+
32+
# Time complexity:
33+
34+
O(n)

Java/NthDigitMaxPrime/MaxPrime.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
imjort java.util.*;
2+
3+
class MaxPrime
4+
{
5+
static int limit = 100000000;
6+
static boolean[] sieve = new boolean[limit + 1];
7+
8+
/*The Sieve of Eratosthenes marks all the no.s except
9+
primes to false in the bool arr in most optimal way.*/
10+
static void SieveOfEratosthenes()
11+
{
12+
for (int i=0;i<limit+1;i++)
13+
{ //setting all values to true
14+
sieve[i]=true;
15+
}
16+
for (int j=2;j*j<=limit;j++)
17+
{
18+
if(sieve[j]==true)
19+
{ //setting all the multiples of j as false
20+
for (int i=j*j;i<=limit;i+=j)
21+
sieve[i]=false;
22+
}
23+
}
24+
}
25+
26+
static int maxPrime(int d)
27+
{
28+
int l=(int)Math.pow(10,d-1); //lowest d digit number
29+
int r=(int)Math.pow(10,d)-1; //highest d digit number
30+
for (int i=r;i>=l;i--)
31+
{ /**The first index with true value in sieve[] will be
32+
the answer as we are traversing in decreasing order */
33+
if(sieve[i])
34+
{
35+
return i;
36+
}
37+
}
38+
39+
return -1;
40+
}
41+
42+
43+
public static void main(String[] args)
44+
{
45+
SieveOfEratosthenes();
46+
47+
Scanner sc=new Scanner(System.in);
48+
int n=sc.nextInt();//Input
49+
50+
System.out.println(maxPrime(n)); //Output
51+
52+
}
53+
}
54+

Java/NthDigitMaxPrime/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Sample Input:
2+
3+
2
4+
5+
6+
7+
# Sample Output:
8+
9+
97
10+
11+
12+
13+
# Sample Input:
14+
15+
6
16+
17+
18+
19+
# Sample Output:
20+
21+
999983
22+
23+
24+
25+
# Explaination:
26+
TC 1:
27+
l=10^(2-1)=10
28+
r=10^2-1=99
29+
30+
After checking in the sieve[] in decreasing order,the first time comes out to be 97
31+
32+
33+
# Time Complexity:
34+
35+
<h3>O(limit log(log limit))</h3>,where limit is the size of the sieve[].This is the most optimal way to find out the required solution.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class PrimeFactorization
5+
{
6+
public static void main(String[] args)
7+
{
8+
Scanner sc=new Scanner(System.in); //Scanner class
9+
10+
int n=sc.nextInt(); //Raeding user input
11+
12+
primeFactorization(n);
13+
}
14+
public static void primeFactorization(int n)
15+
{
16+
/*If n is a factor of 2,we print 2 and
17+
then check whether (n/2)%2 is zero or not*/
18+
while (n%2==0)
19+
{
20+
System.out.print(2+" ");
21+
n=n/2;
22+
}
23+
24+
/*Above loop exits only when n becomes odd,so now n is odd,
25+
so we can skip all the even elements in the for loop i.e i=i+2*/
26+
for (int i=3;i<=Math.sqrt(n);i+=2)
27+
{
28+
/*if i is a factor of n,then print i and then
29+
divide n by i till i is not a factor of i*/
30+
while (n%i==0)
31+
{
32+
System.out.print(i+" ");
33+
n/=i;
34+
}
35+
}
36+
/*condition for the prime numbers
37+
greater than 2,we directly print n*/
38+
if (n>2)
39+
System.out.print(n);
40+
}
41+
42+
43+
}

Java/PrimeFactorization/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Sample Input:
2+
36
3+
4+
# Sample Output:
5+
2 2 3 3
6+
7+
# Sample Input:
8+
11
9+
10+
# Sample Output:
11+
11
12+
13+
# Explaination:
14+
<h3>TC 1: 2 * 2 * 3 * 3 =36</h3>
15+
<h3>TC 2: 11 , since 11 itself is a prime number and a prime factor of itself.</h3>
16+
17+
<p>The while loop and for loop take care of composite numbers and last condition takes care of prime numbers. To prove that the complete algorithm works, we need to prove that steps 1 and 2 actually take care of composite numbers. This is clear that step 1 takes care of even numbers. And after step 1, all remaining prime factor must be odd (difference of two prime factors must be at least 2), this explains why i is incremented by 2.</p>
18+
19+
20+
# Time Complexity:
21+
<h3>O(sqrt(n))</h3>
22+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Relevance in Cryptography:
2+
3+
Cryptography is all about number theory, and all integer numbers (except 0 and 1) are made up of primes, so you deal with primes a lot in number theory.
4+
5+
More specifically, some important cryptographic algorithms such as RSA critically depend on the fact that prime factorization of large numbers takes a long time. Basically you have a "public key" consisting of a product of two large primes used to encrypt a message, and a "secret key" consisting of those two primes used to decrypt the message. You can make the public key public, and everyone can use it to encrypt messages to you, but only you know the prime factors and can decrypt the messages. Everyone else would have to factor the number, which takes too long to be practical, given the current state of the art of number theory.

0 commit comments

Comments
 (0)