Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions Ronak_program/Floyd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Floyd's algorithm to detect cyle in linked list
// c++ codecl

#include <bits/stdc++.h>
using namespace std;

class Node
{
public:
int data;
Node *next;

Node(int data)
{
this->data = data;
next = NULL;
}
};

Node *head = NULL;
class Linkedlist
{
public:
void insert(int value)
{
Node *newNode = new Node(value);
if (head == NULL)
head = newNode;
else
{
newNode->next = head;
head = newNode;
}
}

bool detectLoop()
{
Node *slowPointer = head,
*fastPointer = head;

while (slowPointer != NULL && fastPointer != NULL && fastPointer->next != NULL)
{
slowPointer = slowPointer->next;
fastPointer = fastPointer->next->next;
if (slowPointer == fastPointer)
return 1;
}

return 0;
}
};

int main()
{
// creating linked list
Linkedlist l1;
l1.insert(10);
l1.insert(20);
l1.insert(30);
l1.insert(40);
l1.insert(50);

// creating loop in linked list
Node *temp = head;
while (temp->next != NULL)
temp = temp->next;

temp->next = head;

// determining whether cycle is present or not
if (l1.detectLoop())
cout<<"Loop Found"<< endl;
else
cout<<"Loop does not exists"<< endl;

return 0;
}
Binary file added Ronak_program/Floyd.exe
Binary file not shown.
38 changes: 38 additions & 0 deletions Ronak_program/Wilson.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//Find Prime number using Wilson's theorem
//Chect whether number is prime or not.

#include <iomanip>
#include <iostream>

int factorial_mod(int n, int p) {
int f = 1;
for (; n > 0 && f != 0; --n)
f = (f * n) % p;
return f;
}

bool is_prime(int p) {
return p > 1 && factorial_mod(p - 1, p) == p - 1;
}

int main() {
std::cout << " n | prime?\n------------\n";
std::cout << std::boolalpha;
for (int p : {2, 3, 9, 15, 29, 37, 47, 57, 67, 77, 87, 97, 237, 409, 659})
std::cout << std::setw(3) << p << " | " << is_prime(p) << '\n';

std::cout << "\nFirst 120 primes by Wilson's theorem:\n";
int n = 0, p = 1;
for (; n < 120; ++p) {
if (is_prime(p))
std::cout << std::setw(3) << p << (++n % 20 == 0 ? '\n' : ' ');
}

std::cout << "\n1000th through 1015th primes:\n";
for (int i = 0; n < 1015; ++p) {
if (is_prime(p)) {
if (++n >= 1000)
std::cout << std::setw(4) << p << (++i % 16 == 0 ? '\n' : ' ');
}
}
}
Binary file added Ronak_program/Wilson.exe
Binary file not shown.