1.Find the least positive residue in𝟏𝟏𝟑𝟐 (𝒎𝒐𝒅 𝟐𝟑) .
#include<iostream>
using namespace std;
int main()
{
// by fermats theorem 11^22=1(mod23) finding for 11^10 only
int n=11,p=23,d=10,res=1;
while(d--)
{
res=(res*n)%p;
}
if(res<0)
res+=p;
cout<<"Output:"<<endl;
cout<<res;
OUTPUT
2
2.Find the remainder when 𝟓𝟗𝟎𝟒 is divided by 19
#include<iostream>
using namespace std;
int main()
{
//fermats theroem implies 5^18=1(mod19)
//writing 904=18*q+r
int r=904%18,rem;
if(r<0)
r+=18;
rem=r;
int p=19,n=5,res=1;
while(r--)
{
res=(res*n)%p;
}
if(res<0)
res+=p;
cout<<"904= "<<(904/18)<<"*18+"<<rem<<endl;
cout<<"Output:"<<endl;
cout<<res;
OUTPUT
17
3. Find the digit in the unit and tens place of 𝟏𝟐𝟑𝟐𝟒 .
#include<iostream>
using namespace std;
int main()
{
//units place is given by mod10
int n=12,d=324,u=1,t=1;
for(int i=0;i<d;i++)
{
u=(u*n)%10;
}
if(u<0)
u+=10;
//tens place is mod100/10
for(int i=0;i<d;i++)
t=(t*n)%100;
if(t<0)
t+=100;
t/=10;
cout<<"Output:"<<endl;
cout<<"Unit's Place:"<<u<<endl;
cout<<"Ten's Place:"<<t<<endl;
OUTPUT
Unit’s Place :6
Ten’s Place: 3
4.Find ∅(𝟒𝟑𝟓𝟔) and 𝝈(𝟑𝟐𝟒𝟗)
#include<bits/stdc++.h>
using namespace std;
int main()
{
// Final result of summation of divisors
int sigma = 0,num=3249;
// find all divisors which divides 'num'
for (int i=1; i<=sqrt(num); i++)
{
// if 'i' is divisor of 'num'
if (num%i==0)
{
// if both divisors are same then add
// it only once else add both
if (i==(num/i))
sigma += i;
else
sigma += (i + num/i);
}
}
int phi=0;
num=4356;
for (int i = 1; i < num; i++)
if (__gcd(i, num) == 1)
phi++;
cout<<"Output:"<<endl;
cout<<"Phi:"<<phi<<endl;
cout<<"Sigma:"<<sigma<<endl;
}
OUTPUT
Phi:1320
Sigma:4953
5.Solve the Diaphontine equation 𝟖𝟑𝟕𝒙 + 𝟗𝟐𝟓𝒚 = 𝟐𝟓𝟗𝟗𝟎
#include<iostream>
using namespace std;
int gcdExtended(int a, int b, int *x, int *y)
{
// Base Case
if (a == 0)
{
*x = 0;
*y = 1;
return b;
}
int x1, y1; // To store results of recursive call
int gcd = gcdExtended(b%a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
int main()
{
int a=837,b=925,c=25990;
int g,x,y;
g=gcdExtended(a,b,&x,&y);
if(c%g)
{
cout<<"No solutions";
return 0;
}
x*=(c/g);
y*=(c/g);
cout<<"Output:"<<endl;
cout<<"x="<<x<<" y="<<y;
}
OUTPUT
x=-11747480 y=10629910
6.Solve the system of the congruences 𝒙 ≡ 𝟓(𝒎𝒐𝒅 𝟗)
𝒙 ≡ 𝟏𝟏(𝒎𝒐𝒅 𝟏𝟕)
𝒙 ≡ 𝟏𝟕(𝒎𝒐𝒅 𝟑𝟐) .
#include<iostream>
using namespace std;
int inv(int a, int m)
{
int m0 = m, t, q;
int x0 = 0, x1 = 1;
if (m == 1)
return 0;
// Apply extended Euclid Algorithm
while (a > 1)
{
// q is quotient
q = a / m;
t = m;
// m is remainder now, process same as
// euclid's algo
m = a % m, a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
// Make x1 positive
if (x1 < 0)
x1 += m0;
return x1;
}
// number x such that:
// x % num[0] = rem[0],
// x % num[1] = rem[1],
// ..................
// x % num[k-2] = rem[k-1]
int findMinX(int num[], int rem[], int k)
{
// Compute product of all numbers
int prod = 1;
for (int i = 0; i < k; i++)
prod *= num[i];
// Initialize result
int result = 0;
// Apply above formula
for (int i = 0; i < k; i++)
{
int pp = prod / num[i];
result += rem[i] * inv(pp, num[i]) * pp;
}
return result % prod;
}
int main()
{
int num[]={9,17,32},rem[]={5,11,17};
int x=findMinX(num,rem,3);
cout<<"Output:"<<endl;
cout<<"x="<<x;
OUTPUT
x=113
7.Find the order of 𝟕(𝒎𝒐𝒅 𝟑𝟏)
#include<iostream>
using namespace std;
int main()
{
//finding the first power when 7^d=1mod(31)
int p=31,n=7,phi=30,res=1,d;
for(int i=1;;i++)
{
res=(res*n)%p;
if(res<0)
res+=p;
if(res==1)
{ d=i;
break;
}
}
cout<<"Output:"<<endl;
cout<<"Order is: "<<d;
}
OUTPUT
Order is: 15
8.Find the primitive roots of 29, 37.
#include<bits/stdc++.h>
using namespace std;
int power(int x, unsigned int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x*x) % p;
}
return res;
}
// Utility function to store prime factors of a number
void findPrimefactors(unordered_set<int> &s, int n)
{
// Print the number of 2s that divide n
while (n%2 == 0)
{
s.insert(2);
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i+2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
s.insert(i);
n = n/i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
s.insert(n);
}
int findPrimitive(int n)
{
unordered_set<int> s;
// Find value of Euler Totient function of n
// Since n is a prime number, the value of Euler
// Totient function is n-1 as there are n-1
// relatively prime numbers.
int phi = n-1;
// Find prime factors of phi and store in a set
findPrimefactors(s, phi);
// Check for every number from 2 to phi
for (int r=2; r<=phi; r++)
{
// Iterate through all prime factors of phi.
// and check if we found a power with value 1
bool flag = false;
for (auto it = s.begin(); it != s.end(); it++)
{
// Check if r^((phi)/primefactors) mod n
// is 1 or not
if (power(r, phi/(*it), n) == 1)
{
flag = true;
break;
}
}
// If there was no power with value 1.
if (flag == false)
cout<<r<<" ";
}
// If no primitive root found
return -1;
}
int main()
{
int n1=29,n2=37;
cout<<"Primitive roots of "<<n1<<endl;
int c=findPrimitive(n1);
cout<<endl;
cout<<"Primitive roots of "<<n2<<endl;
c=findPrimitive(n2);
}
OUTPUT
9.Find solutions of the congruence 𝒙𝟒 = 𝟕(𝒎𝒐𝒅 𝟑𝟕) .
#include<bits/stdc++.h>
using namespace std;
int main()
{
int p=37,r;
cout<<"Solutions are:"<<endl;
for(int i=1;i<=p;i++)
{
r=(i*i*i*i)%p;
if(r<0)
r+=p;
if(r==7)
cout<<i<<" ";
}
OUTPUT
Solutions are:
3 18 19 34
10.Find the quadratic residues of 17.
#include<bits/stdc++.h>
using namespace std;
int main()
{
//17 is an odd prime so 0^2,1^2,...,(p-1/2)^2 are the q.r
int p=17,r;
cout<<"Q.r's of 17 are:"<<endl;
for(int i=0;i<=(p-1)/2;i++)
{
r=(i*i)%p;
if(r<0)
r+=p;
cout<<r<<" ";
}
OUTPUT
Q.r's of 17 are:
0 1 4 9 16 8 2 15 13
11.Find the continued fraction of 357/183.
#include<iostream>
using namespace std;
int arr[1000];
int main()
{
int p=357,q=183,ind=0,t;
while(p&&q)
{
arr[ind++]=p/q;
p=(p-(q*(p/q)));
t=q;
q=p;
p=t;
cout<<"Output:"<<endl;
for(int i=0;i<ind;i++)
cout<<arr[i]<<" ";
OUTPUT
1 1 19 3
Lab Problems
Number Theory
Gampa
Phanideep
IDD Part-IV
15123004