diff --git a/Ronak_program/Floyd.cpp b/Ronak_program/Floyd.cpp new file mode 100644 index 0000000..01d45ee --- /dev/null +++ b/Ronak_program/Floyd.cpp @@ -0,0 +1,77 @@ +// Floyd's algorithm to detect cyle in linked list +// c++ codecl + +#include +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; +} diff --git a/Ronak_program/Floyd.exe b/Ronak_program/Floyd.exe new file mode 100644 index 0000000..d104361 Binary files /dev/null and b/Ronak_program/Floyd.exe differ diff --git a/Ronak_program/Wilson.cpp b/Ronak_program/Wilson.cpp new file mode 100644 index 0000000..4823d76 --- /dev/null +++ b/Ronak_program/Wilson.cpp @@ -0,0 +1,38 @@ +//Find Prime number using Wilson's theorem +//Chect whether number is prime or not. + +#include +#include + +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' : ' '); + } + } +} \ No newline at end of file diff --git a/Ronak_program/Wilson.exe b/Ronak_program/Wilson.exe new file mode 100644 index 0000000..9f34b29 Binary files /dev/null and b/Ronak_program/Wilson.exe differ