logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Implement Solovay-Strassen Primality Test

Posted on January 1, 2014 by Manish
This C++ Program demonstrates the implementation of Solovay-Strassen Primality Test.

Here is source code of the C++ Program to demonstrate the implementation of Solovay-Strassen Primality Test. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C++ Program to Implement Solovay-Strassen Primality Test
  3.  */
  4. #include <cstring>
  5. #include <iostream>
  6. #include <cstdlib>
  7. #define ll long long
  8. using namespace std;
  9. /* 
  10.  * modular exponentiation
  11.  */
  12. ll modulo(ll base, ll exponent, ll mod)
  13. {
  14.     ll x = 1;
  15.     ll y = base;
  16.     while (exponent > 0)
  17.     {
  18.         if (exponent % 2 == 1)
  19.             x = (x * y) % mod;
  20.         y = (y * y) % mod;
  21.         exponent = exponent / 2;
  22.     }
  23.     return x % mod;
  24. }
  25. /* 
  26.  * calculates Jacobian(a/n) n>0 and n is odd 
  27.  */
  28. int calculateJacobian(ll a,ll n)
  29. {
  30.     if (!a) 
  31.         return 0;
  32.     int ans = 1;
  33.     ll temp;
  34.     if (a < 0)
  35.     {
  36.         a = -a;
  37.         if (n % 4 == 3) 
  38.             ans=-ans; 
  39.     }
  40.     if (a == 1) 
  41.         return ans;
  42.     while (a)
  43.     {
  44.         if (a < 0)
  45.         {
  46.             a = -a;
  47.             if (n % 4 == 3) 
  48.                 ans = -ans;  
  49.         }
  50.         while (a % 2 == 0)
  51.         {
  52.             a = a / 2;
  53.             if (n % 8 == 3 || n % 8 == 5) 
  54.                 ans = -ans;    
  55.         }
  56.         swap(a, n);
  57.         if (a % 4 == 3 && n % 4 == 3) 
  58.             ans = -ans;
  59.         a = a % n;
  60.         if (a > n / 2) 
  61.             a = a - n; 
  62.     }
  63.     if (n == 1) 
  64.         return ans;
  65.     return 0; 
  66. }
  67.  
  68. /* 
  69.  * Solovay-Strassen Primality Test
  70.  * Iterations determine the accuracy of the test 
  71.  */
  72. bool Solovoy(ll p, int iteration)
  73. {
  74.     if (p < 2) 
  75.         return false;
  76.     if (p != 2 && p % 2 == 0) 
  77.         return false;
  78.     for (int i = 0; i < iteration; i++)
  79.     {
  80.         ll a = rand() % (p - 1) + 1;
  81.         ll jacobian = (p + calculateJacobian(a, p)) % p;
  82.         ll mod = modulo(a, (p - 1) / 2, p);
  83.         if (!jacobian || mod != jacobian)
  84.         { 
  85.             return false;
  86.         }
  87.     }
  88.     return true;
  89. }
  90. //Main
  91. int main()
  92. {
  93.     int iteration = 50;
  94.     ll num;
  95.     cout<<"Enter integr to test primality: ";
  96.     cin>>num;
  97.     if (Solovoy(num, iteration))
  98.         cout<<num<<" is prime"<<endl;
  99.     else
  100.         cout<<num<<" is not prime"<<endl;
  101.     return 0;
  102. }

$ g++ solovay_strassen.cpp
$ a.out
 
Enter integr to test primality: 219891801103773
219891801103773 is not prime
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

If you wish to look at all C++ Programming examples, go to C++ Programs.
« Prev Page - C++ Program to Implement Stable Marriage Problem
» Next Page - C++ Program to Implement Ternary Heap
« C++ Program to Implement Stable Marriage Problem
C++ Program to Implement Ternary Heap »

Deep Dive @ Sanfoundry:

  1. LISP Questions and Answers
  2. C# Programming Examples on Inheritance
  3. C# Programming Examples on Mathematics
  4. C Programming Examples on Numerical Problems & Algorithms
  5. Java Programming Examples on Numerical Problems & Algorithms
  6. C++ Programming Examples on Numerical Problems & Algorithms
  7. C Program to Implement Park-Miller Random Number Generation Algorithm
  8. Java Program to Implement Strassen Algorithm
  9. C Program to Implement Strassen’s Algorithm
  10. Java Program to Implement Miller Rabin Primality Test Algorithm
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry