You are on page 1of 28

NUMBER SYSTEMS

Implementation of Number Systems using C++

BY
YOGESH GORE (152190013)
ANKUSH CHANDOLE (152190015)
AKSHAY GUPTA (152190018)
RAZIQUE DAKHNI (152190021)
PRANJAL GORE (152190023)

VJTI M.TECH First Year Software Engineering 2015-17

Abstract
This project establishes the basic concepts under the topic of Number Systems viz.
GCD, Fermat Theorem, Divisibility, Chinese Remainder Theorem and RSA Algorithm.
The user is presented with an option of selecting which particular module out of the
five modules is to be selected for execution. After the successful execution of the
selected module, the user is again presented with an option to select one out of the
five modules. In this sense, the program is iterative in nature.

Introduction
A numeral system (or system of numeration) is a writing system for
expressing numbers, that is, a mathematical notation for representing numbers of a
given set, using digits or other symbols in a consistent manner. It can be seen as
the context that allows the symbols "11" to be interpreted as the binary symbol for
three, the decimal symbol for eleven, or a symbol for other numbers in different
bases. There are five modules that we are to implement in the project on Number
System; viz. Greatest Common Divisor (GCD), Fermat Theorem, Divisibility, Chinese
Remainder Theorem and RSA Algorithm.
In Number System, the greatest common divisor (GCD) of two or
more integers, when at least one of them is not zero, is the largest positive integer
that divides the numbers without a remainder. The GCD is also known as
the Greatest Common Factor (GCF), Highest Common Factor (HCF), Greatest
Common Measure (GCM), or highest common divisor (HCF).
Fermat's little theorem is the basis for the fundamental results of elementary
number theory. The theorem is named after Pierre de Fermat, who stated it in 1640.
It is called the "little theorem" to distinguish it from Fermat's last theorem.
A divisibility rule is a shorthand way of determining whether a given number
is divisible by a fixed divisor without performing the division, usually by examining
its digits.
The Chinese remainder theorem is a result about similarities in number
theory and its generalizations in abstract algebra. It was first published in the 3rd to
5th centuries by the Chinese mathematician Sun Tzu. In its basic form, the Chinese
remainder theorem will determine a number n that, when divided by some given
divisors, leaves given remainders.
RSA is one of the first practical public-key cryptosystems and is widely used
for secure data transmission. In such a cryptosystem, the encryption key is public
and differs from the decryption key which is kept secret. In RSA, this asymmetry is
based on the practical difficulty of factoring the product of two large prime
numbers, the factoring problem. RSA is made of the initial letters of the surnames of
Ron Rivest, Adi Shamir, and Leonard Adleman, who first publicly described the
algorithm in 1977.

Motivation
In our Master program for the course Computational Methods, we have
learned various methods related to problem solving in order to explore the basics of
computing, including the use of computer software and hardware to solve problems
across a range of disciplines and gain expertise in applying computing to the area of
major study.
Hence, we decided to use various computation methods that were used by
each of the five algorithms in order to achieve the same output.

Theory
Greatest Common Divisor (GCD)
In mathematics, the greatest common divisor (GCD) of two or more integers, when
at least one of them is not zero, is the largest positive integer that divides the
numbers without a remainder.
The GCD is also known as the Greatest Common Factor (GCF), Highest Common
Factor (HCF), Greatest Common Measure (GCM), or highest common divisor (HCF).
The greatest common divisor is useful for:

Reducing fractions to be in lowest terms


Coprime numbers
A geometric view

Example:
The number 54 can be expressed as a product of two integers in several different
ways:

54 1=27 2=18 3=9 6


Thus the divisors of 54 are:

1,2, 3, 6, 9,18, 27,54

Similarly the divisors of 24 are:

1,2, 3, 4, 6, 8,12, 24

The numbers that these two lists share in common are the common divisors of 54
and 24:

1,2, 3, 6

The greatest of these is 6. That is the greatest common divisor of 54 and 24. One
writes:

gcd ( 54, 24 ) =6
Algorithm
Input: two integers a and b greater than zero
Output: GCD of a and b
function gcd(a, b)
while b 0
t := b
b := a mod b
a := t
return a

Code
/** This program is used for finding GCD of two integers.
* @Created on 7th Oct 2015
* @Author : Razique Dakhni, 152190021.
* Version: 1.0
**/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int prime, no;
//prime and no are two integers of which we need to find the GCD
cout<<"GCD - "<<endl;
cout<<"Enter two numbers : "<<endl;
cin>>prime;
cin>>no;
cout<<"GCD of "<<no<<" and "<<prime<<" is "<<__gcd(no,prime)<<endl;
return 0;
}

Output
3

Fermat Theorem
Statement
This theorem states that, if 'p' is a prime number and 'a' is an integer then

a( p1)=1(mod p)
Proof
Consider

'

'a

of numbers as

is positive and not divisible by

a , 2 a ,3 a , , ( p1 ) a

' '

p . So, we can write the sequence

reduce each one modulo p.

The resulting sequence rearrangement of the form is,

1,2, 3, , ( p1 ) .

By the FERMAT'S theorem,


Multiply the numbers in each sequence, the result must be identical

a 2 a 3 a ( p1 ) a 1 2 3 ( p1 ) ( mod p )

( 1 2 3 p1 ) ( a a a a ) 1 2 3 ( p1 ) ( mod p )

modulo of p ,

( p1 ) ! a( p1) ( p1 ) !(mod p)

Cancellation of

( p1 ) ! , on both sides in the above

equation which gives,

( p1)

1(mod p)

Example
Let us consider,

a=8, p=9

The sequence of numbers are,

8, 16,24, 32, 40, 48,56, 64


Reduce mod 9, then the sequence of numbers are,

8, 7,6, 5, 4,3, 2, 1
By rearranging the above sequence,

1,2, 3, 4, 5,6, 7, 8
From the theorem,

8 16 24 32 40 48 56 64 1 2 3 4 5 6 7 8(mod 9)
(8 8 8 8 8 8 8 8) (1 2 3 4 5 6 7 8)1 2 3 4 5 6 7 8(mod 9)
88 (1 2 3 4 5 6 7 8)1 2 3 4 5 6 7 8(mod 9)
Cancelling on both sides,

88 1(mod 9)

[By the theorem

( p1)

Hence the Fermat's Little Theorem is proved.

Algorithm
Input: one integer a and one prime number b
Output: True if the input follows Fermat Theorem else false.
function fermat(a, b)
if(b is prime)
tmp =

ab ;

tmp = tmp a;
if(temp % b == 0)
return true;
5

(mod p) ]

else return false;


endif

Code
/** This program is an implementation of fermat theorem
* @Created on 9th Oct 2015
* @Author : Pranjal Gore, 152190023.
* Version: 1.0
**/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
cout<<"Enter an Integer and a prime number : ";
cin>>no;
cin>>prime;
if(!isPrime(int(prime)))
{
double tmp = pow(no, prime);
tmp = tmp - no;
if(int (tmp) % int(prime) == 0)
cout<<no<<" and "<<prime<<" satisfy Fermat Theorem."<<endl;
else cout<<"Error: "<<no<<" and "<<prime<<" do not satisfy Fermat
Theorem. "<<endl;
}
return 0;
}

Output

Divisibility
Rule for 2

If the number ends in 2,4,6,8,or 0 it can be divided by 2.


1. 74,974,764, The number ends with a even number therefore 174,974,764 can be
divided equally by 2.
2. 549,268, The number ends with a 8,so 549,268 can be equally divided by 2.
Rule for 3

If the sum of the digits is divisible by 3,the number is also.


1. 13,210 (3+2+1+0=6) Therefore 3,210 is divisible by 3.
2. 846,555,036 (8+4+6+5+5+5+0+3+6=42) Therefore 846,555,102 is divisible by
3.
Rule for 4

If the last two digits are a multiple of four then the number can be divided by four.
1. 1,348, 48 is a multiple of 4 therefore 1,348 is divisible by 4.
2. 27,616, 16 is a multiple of 4 therefore 27,616 is divisible by 4.
Rule for 5

If the last digit ends with 5 or 0.


1. 1,960, 60 can be divided by 5 so, 1,960 is divisible by 5.
2. 38,095, 5 can be divided by 5 so, 38,095 is divisible by 5.
Rule for 6

A number is divisible by six if it can follow the divisibility rule for 2 and 3.
1. 636 6 is a multiple of 2 and6+3+6=15 and 15 is a multiple of 3 so 636 can be
equally divided by 6.
2. 21,012 2 is a multiple of 2 and 2+1+1+2=6 and 6 is a multiple of 3 so 21,012 is
divisible by 6.
Rule for 7

If the last digit in the number is doubled and then subtracted from the rest of the
digits.
1. 672 (2+2=4) 67-4=63 so 672 can be divided equally by 7.
2. 357 (7+7=14) 35-14=21 therefore 357 can be divided by 7.
Rule for 8

If the last 3 digits are divisible by 8, so is the entire number.


1. 6008; 008 is divisible by 8 so 6008 is divisible by 8.
2. 587,320; 320 is divisible by 8 therefore 587,320 is divisible by 8.
Rule for 9

If the sum of the digits is evenly divided by 9.


1. 4,598 (4+9+5+8=27) and 27 divided by 3=9 therefore 4,598 is divisible by 9.
2. 9,999 (9+9+9+9=36) 36 divided by 4 is 9 therefore 9,999 is divisible by 9.

Algorithm
Input: any integer a
Output: All the numbers in the range [1,10] which divide a
function div(a)
function byTwo(a)
if(last digit of a is divisible by 2)
return true;
function byThree(a)
if(sum of all digits of a are divisible by 3)
return true;
function byFour(a)
if(last two digits of a are divisible by 4)
return true;
function byFive(a)
if(last digit is zero or five)
return true;
function bySix(a)
if(a is divisible by 2 and 3)
8

return true;
function bySeven(a)
r := last digit of a;
x := a/10;
r := 2*r;
r = x r;
if(r%7 == 0)
return true;
function byEight(a)
if(last three digits are divisible by 8)
return true;
function byNine(a)
if(Sum of digits is divisible by 9)
return true;
function byTen(a)
if(last digit is zero)
return true;

Code
/** This program is an implementation of rules of divisibility
* @Created on 9th Oct 2015
* @Author : Akshay Gupta, 152190018.
* Version: 1.0
**/
#include<iostream>
using namespace std;
int r=0, sum=0;
/** r is used to hold the last digits of the integer a
* sum is to hold the sum of required digits of the integer a
*/
bool byTwo(int x)
{
r = x%10;
if(r==0 || r==2 || r==4 || r==6 || r==8)
return true;
else return false;
}

bool byThree(int x)
{
if(x<10)
sum=x;
while(x/10>0)
{
r = x%10;
x = x/10;
sum = r + x%10;
}
if(sum==3|| sum==6|| sum==9)
return true;
else return false;
}
bool byFour(int x)
{
r = x%100;
if(r%4 == 0)
return true;
else return false;
}
bool byFive(int x)
{
r = x%10;
if(r==0||r==5)
return true;
else return false;
}
bool bySix(int x)
{
if(byTwo(x) && byThree(x))
return true;
else return false;
}
bool bySeven(int x)
{
r = x%10;
x = x/10;
r = 2*r;
10

r = x-r;
if(r%7==0)
return true;
else return false;
}
bool byEight(int x)
{
r = x%1000;
if(r%8 == 0)
return true;
else return false;
}
bool byNine(int x)
{
if(x<10)
sum=x;
while(x/10>0)
{
r = x%10;
x = x/10;
sum = r + x%10;
}
if(sum==9)
return true;
else return false;
}
bool byTen(int x)
{
r = x%10;
if(r == 0)
return true;
else return false;
}
int main()
{
int a;
cout<<"Enter the Number : "<<endl;
cin>>a;

11

if(byTwo(a))
cout<<a<<" is divisible by two"<<endl;
if(byThree(a))
cout<<a<<" is divisible by three"<<endl;
if(byFour(a))
cout<<a<<" is divisible by four"<<endl;
if(byFive(a))
cout<<a<<" is divisible by five"<<endl;
if(bySix(a))
cout<<a<<" is divisible by six"<<endl;
if(bySeven(a))
cout<<a<<" is divisible by seven"<<endl;
if(byEight(a))
cout<<a<<" is divisible by eight"<<endl;
if(byNine(a))
cout<<a<<" is divisible by nine"<<endl;
if(byTen(a))
cout<<a<<" is divisible by ten"<<endl;
return 0;
}

Output

12

The Chinese Remainder Theorem


Introduction
The Chinese remainder theorem is a result about similarities in number theory and
its generalizations in abstract algebra. It was first published in the 3rd to 5th
centuries by the Chinese mathematician Sun Tzu.
In its basic form, the Chinese remainder theorem will determine a number n that,
when divided by some given divisors, leaves given remainders.

Algorithm
Suppose that

a1 , a2 , , ar

m1 , m2 , , mr

are pairwise relatively prime positive integers, and let

be integers. Then the system of similarities,

has a unique solution modulo

x ai ( mod mi ) for 1 i r ,

M=m1 m 2 m r , which is given by:

x a1 M 1 y 1+ a2 M 2 y 2+ +ar M r y r ( mod M )

where

Example

13

M i=

M
y i M 1
i ( mod mi ) for 1 i r
mi

Find the smallest multiple of 10 which has remainder 2 when divided by 3, and
remainder 3 when divided by 7. We are looking for a number which satisfies the
similarities,

x 3 mod7

x 2mod 3
,

and

x 0 mod 2 ,

x 0 mod 5 . Since, 2, 3, 5 and 7 are all relatively

prime in pairs, the Chinese Remainder Theorem tells us that there is a unique
solution

modulo 210 ( 2 3 5 7 ) .

We calculate the

M 'i s

and

yi ' s

M 2=

210
=105; y 2 ( 105 )1 ( mod 2 )=1
2

M 3=

210
=70 ; y3 ( 70 )1 ( mod 3 )=1
3

M 5=

210
=42 ; y 5 ( 42 )1 ( mod5 )=3
5

M 7=

210
1
=30 ; y 7 ( 30 ) ( mod 7 )=4
7
So,

as follows:

x 0 ( M 2 y 2 ) +2 ( M 3 y 3 ) +0 ( M 5 y 5 ) +3 ( M 7 y 7 ) 0+2 ( 70 )( 1 ) +0+3 ( 30 ) ( 4 )

x 140+ 360 500 mod 210 80

Code
/** This program is an implementation of Chinese Remainder Theorem
* @Created on 15th Oct 2015
* @Author : Ankush Chandole, 152190015.
* Version: 1.0
**/
#include<iostream>
#include<math.h>
using namespace std;
14

long
long
long
long
long
long
long
long
long
long
long

int
int
int
int
int
int
int
int
int
int
int

bigM=0;
A1;
A2;
A3;
m1;
m2;
m3;
inv1;
inv2;
inv3;
x;

long modinv(long A, long p)


{
long a, b, q, t, x, y;
a = p;
b = A;
x = 1;
y = 0;
while (b != 0) {
t = b;
q = a/t;
b = a - q*t;
a = t;
t = x;
x = y - q*t;
y = t;
}
return (y < 0) ? y+p : y;
}
int main()
{
cout << "\nPlease enter these values: x = a ( mod m )\n\n";
cout << "Enter the values of A1, A2, A3\n\n";
cout << "A1: ";
cin >> A1;
cout << "A2: ";
cin >> A2;
cout << "A3: ";
cin >> A3;
15

cout << "\n";


cout << "\nPlease enter the values of M1,M2,M3 **Must Be Prime**\n\n";
cout << "M1: ";
cin >> m1;
cout << "M2: ";
cin >> m2;
cout << "M3: ";
cin >> m3;
cout << "\n";
bigM=(m1*m2*m3);
inv1=modinv(A1,m1);
inv2=modinv(A2,m2);
inv3=modinv(A3,m3);
x=( (A1*(bigM/m1)*inv1) + (A2*(bigM/m2)*inv2) + (A3*(bigM/m3)*inv3) ) % bigM;
cout << "The Chinese Remainder (x) is ";
cout << x;
cout << "\n";
return 0;
}

Output

16

RSA
RSA is one of the first practical public-key cryptosystems and is widely used
for secure data transmission. In such a cryptosystem, the encryption key is public
and differs from the decryption key which is kept secret. In RSA, this asymmetry is
based on the practical difficulty of factoring the product of two large prime
numbers, the factoring problem. RSA is made of the initial letters of the surnames of
Ron Rivest, Adi Shamir, and Leonard Adleman, who first publicly described the
algorithm in 1977.

17

Algorithm
Public key Kv = (e , n)
Private key Kr = (d , n)
Step 1:
Choose two prime number p and q.
Step 2:
Calculate the value of n.
n=p*q
Step 3:
(p q) = (p-1) (q-1)
Where is the euler totient function.
Step 4:
Choose d such that
d * e = 1 (mod )
where, 1 < e <
e should be prime and e and q should be co-prime
Step 5:
Calculate the value of d using extended euclidian algorithm
Step 6:
Encryption of plaintext p the resultant value
c = pe(mod n)
Step 7:
Decryption of c at receiving end to get
p = cd(mod n)

Code
/** This program is an implementation of RSA Algorithm
* @Created on 10th Oct 2015
* @Author : Yogesh Gore, 152190013.
* Version: 1.0
**/
18

#include<math.h>
#include<fstream>
#include<cstring>
using namespace std;
long int p,q,n,t,flag1,j,i,e[100],d[100],temp[100],m[100],en[100];
int k;
string path;
char msg[100];
int primeno(long int pr)
{
int i;
j=sqrt(pr);
for(i=2;i<=j;i++)
{
if(pr % i == 0)
return 0;
}
return 1;
}

void input()
{
cout << "\n Enter first prime number\n" ;
cin >> p;
flag1 = primeno(p);
if(flag1==0)
19

{
cout << "\n wrong input\n";
exit(1);
}
cout << "\n Enter another prime number\n";
cin >>q;
flag1 = primeno(q);
if(flag1 == 0 || p == q)
{
cout << "\nwrong input\n";
exit(1);
}
cout << "\nEnter message\n";
fflush(stdin);
cin >>msg;
for(i=0; msg[i] != NULL ;i++)
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();
cout << "\nPossible values of e and d are\n";
for(i=0 ; i<j ; i++)
cout << e[i] << "\t" << d[i] << "\n";
20

encrypt();
}
void ce()
{
int k;
k=0;
for(i=2;i<t;i++)
{
if (t % i == 0)
continue;
flag1 = primeno(i);
if (flag1 == 1 && i != p && i != q)
{
e[k] = i;
flag1 = cd(e[k]);
if (flag1 > 0)
{
d[k] = flag1;
k++;
}
if (k == 99)
break;
}
21

}
}
long int cd(long int x)
{
long int k = 1;
while (1)
{
k = k + t;
if (k % x == 0)
return (k / x);
}
}
void encrypt()
{
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while (i != len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for (j = 0; j < key; j++)
{
22

k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
cout << "\nTHE ENCRYPTED MESSAGE IS\n\n" << "\t\t" ;
for (i = 0; en[i] != -1; i++)
printf("%c ",en[i]);
ofstream fileObj("C:/Users/Pranjal/Desktop/crypt.dat",ios::out);
for(i=0;en[i]!=-1;i++)
fileObj<<static_cast<char>(en[i])<<" ";
}
void decrypt()
{
long int pt, ct, key = d[0], k;
i = 0;
while (en[i] != -1)
23

{
ct = temp[i];
k = 1;
for (j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
cout << "\nTHE DECRYPTED MESSAGE IS\n\n" <<"\t\t";
for (i = 0; m[i] != -1; i++)
printf("%c ",m[i]);
cout << "\n" ;
}
void finput()
{
ifstream fileObj("C:/Users/Pranjal/Desktop/crypt.dat",ios::in);
decrypt();
}

24

int main()
{
cout<<"What would you like to do?"<<endl;
cout<<"1. Encrypt"<<endl;
cout<<"2. Decrypt"<<endl;
cin>>k;
switch(k)
{
case 1: input(); break;
case 2: finput();
break;
}
return 0;
}

Output

25

26

27

You might also like