summaryrefslogtreecommitdiff
path: root/3808/CH4
diff options
context:
space:
mode:
Diffstat (limited to '3808/CH4')
-rw-r--r--3808/CH4/EX4.1/Ex4_1.sce13
-rw-r--r--3808/CH4/EX4.10/Ex4_10.sce34
-rw-r--r--3808/CH4/EX4.11/Ex4_11.sce17
-rw-r--r--3808/CH4/EX4.12/Ex4_12.sce25
-rw-r--r--3808/CH4/EX4.13/Ex4_13.sce18
-rw-r--r--3808/CH4/EX4.14/Ex4_14.sce17
-rw-r--r--3808/CH4/EX4.15/Ex4_15.sce21
-rw-r--r--3808/CH4/EX4.16/Ex4_16.sce21
-rw-r--r--3808/CH4/EX4.2/Ex4_2.sce13
-rw-r--r--3808/CH4/EX4.3/Ex4_3.sce17
-rw-r--r--3808/CH4/EX4.4/Ex4_4.sce15
-rw-r--r--3808/CH4/EX4.5/Ex4_5.sce46
-rw-r--r--3808/CH4/EX4.6/Ex4_6.sce17
-rw-r--r--3808/CH4/EX4.7/Ex4_7.sce38
-rw-r--r--3808/CH4/EX4.8/Ex4_8.sce25
-rw-r--r--3808/CH4/EX4.9/Ex4_9.sce25
16 files changed, 362 insertions, 0 deletions
diff --git a/3808/CH4/EX4.1/Ex4_1.sce b/3808/CH4/EX4.1/Ex4_1.sce
new file mode 100644
index 000000000..67e8c11b7
--- /dev/null
+++ b/3808/CH4/EX4.1/Ex4_1.sce
@@ -0,0 +1,13 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+//To find the quotient and remainder
+
+dividend=101
+divisor=11
+quotient=int(dividend/divisor) //To find quotient
+remainder=modulo(dividend,divisor) //To find remainder
+dividend_a=(divisor *quotient)+remainder //To find dividend
+mprintf("The quotient when %d is divided by %d is %d = %d div %d and the remainder is %d = %d mod %d",dividend,divisor,quotient,dividend,divisor,remainder,dividend,divisor)
diff --git a/3808/CH4/EX4.10/Ex4_10.sce b/3808/CH4/EX4.10/Ex4_10.sce
new file mode 100644
index 000000000..1cb348579
--- /dev/null
+++ b/3808/CH4/EX4.10/Ex4_10.sce
@@ -0,0 +1,34 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+function primefactors(n)
+ while modulo(n,2) == 0 //To print all the 2s that divide input
+ disp('2')
+ n=n/2
+ end
+ for i=3:2:sqrt(n)//increment by 2 so as to obtain odd numbers only
+ while modulo(n,i)==0
+ disp(i)
+ n=n/i
+ end
+ end
+if(n>2) then //to check for prime number
+ disp(n)
+ end
+endfunction
+
+n1=100
+n2=641
+n3=999
+n4=1024
+mprintf("Prime factors of %d are:",n1)
+disp(primefactors(n1))
+mprintf("\nPrime factors of %d are:",n2)
+disp(primefactors(n2))
+mprintf("\nPrime factors of %d are:",n3)
+disp(primefactors(n3))
+mprintf("\nPrime factors of %d are:",n4)
+disp(primefactors(n4))
+
diff --git a/3808/CH4/EX4.11/Ex4_11.sce b/3808/CH4/EX4.11/Ex4_11.sce
new file mode 100644
index 000000000..c50d792be
--- /dev/null
+++ b/3808/CH4/EX4.11/Ex4_11.sce
@@ -0,0 +1,17 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+n=input("Enter the number:")
+c=0
+for i =2:n-1
+ if modulo(n,i)==0 then
+ c=c+1
+ end
+end
+if c==0 then
+ mprintf("%d is a prime number",n)
+else
+ mprintf("%d is not a prime number",n)
+end
diff --git a/3808/CH4/EX4.12/Ex4_12.sce b/3808/CH4/EX4.12/Ex4_12.sce
new file mode 100644
index 000000000..b9dae24eb
--- /dev/null
+++ b/3808/CH4/EX4.12/Ex4_12.sce
@@ -0,0 +1,25 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+function primefactors(n)
+ while modulo(n,2) == 0 //To print all the 2s that divide input
+ disp('2')
+ n=n/2
+ end
+ for i=3:2:sqrt(n)//increment by 2 so as to obtain odd numbers only
+ while modulo(n,i)==0
+ disp(i)
+ n=n/i
+ end
+ end
+if(n>2) then //to check for prime number
+ disp(n)
+ end
+endfunction
+
+n1=7007
+mprintf("Prime factors of %d are:",n1)
+disp(primefactors(n1))
+
diff --git a/3808/CH4/EX4.13/Ex4_13.sce b/3808/CH4/EX4.13/Ex4_13.sce
new file mode 100644
index 000000000..3f0b75d2b
--- /dev/null
+++ b/3808/CH4/EX4.13/Ex4_13.sce
@@ -0,0 +1,18 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+//GCD using recursion
+function f=gcd(n,m)
+ if (n>=m) & (modulo(n,m)==0) then
+ f=m
+ else
+ f=gcd(m,modulo(n,m))
+ end
+endfunction
+
+a=input("Number 1:")
+b=input("Number 2:")
+ann=gcd(a,b)
+mprintf("GCD(%d,%d) is:%d",a,b,ann)
diff --git a/3808/CH4/EX4.14/Ex4_14.sce b/3808/CH4/EX4.14/Ex4_14.sce
new file mode 100644
index 000000000..ecf0c7486
--- /dev/null
+++ b/3808/CH4/EX4.14/Ex4_14.sce
@@ -0,0 +1,17 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+n1=input("Number 1:")
+n2=input("Number 2:")
+a=n1
+b=n2
+while n1 ~=n2
+ if n1>n2 then
+ n1=n1-n2
+ else
+ n2=n2-n1
+ end
+end
+mprintf("GCD(%d,%d) is:%d",a,b,n1)
diff --git a/3808/CH4/EX4.15/Ex4_15.sce b/3808/CH4/EX4.15/Ex4_15.sce
new file mode 100644
index 000000000..894d8d040
--- /dev/null
+++ b/3808/CH4/EX4.15/Ex4_15.sce
@@ -0,0 +1,21 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+//To find the GCD using euclidean algorithm
+
+function gcd(a,b)
+ x=a
+ y=b
+ while y ~=0
+ r=modulo(x,y)
+ x=y
+ y=r
+ end
+mprintf("GCD(%d,%d) = %d",a,b,x)
+endfunction
+
+n1=input("Enter 1st Number:")
+n2=input("Enter 2nd Number:")
+gcd(n1,n2)
diff --git a/3808/CH4/EX4.16/Ex4_16.sce b/3808/CH4/EX4.16/Ex4_16.sce
new file mode 100644
index 000000000..894d8d040
--- /dev/null
+++ b/3808/CH4/EX4.16/Ex4_16.sce
@@ -0,0 +1,21 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+//To find the GCD using euclidean algorithm
+
+function gcd(a,b)
+ x=a
+ y=b
+ while y ~=0
+ r=modulo(x,y)
+ x=y
+ y=r
+ end
+mprintf("GCD(%d,%d) = %d",a,b,x)
+endfunction
+
+n1=input("Enter 1st Number:")
+n2=input("Enter 2nd Number:")
+gcd(n1,n2)
diff --git a/3808/CH4/EX4.2/Ex4_2.sce b/3808/CH4/EX4.2/Ex4_2.sce
new file mode 100644
index 000000000..67873dde0
--- /dev/null
+++ b/3808/CH4/EX4.2/Ex4_2.sce
@@ -0,0 +1,13 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+//To find the quotient and remainder
+
+dividend=-11
+divisor=3
+quotient=(dividend/divisor) //To find quotient
+remainder=pmodulo(dividend,divisor) //To find remainder
+dividend_a=(divisor*quotient)+remainder //To find dividend
+mprintf("The quotient when %d is divided by %d is %.f = %d div %d and the remainder is %d = %d mod %d",dividend,divisor,quotient,dividend,divisor,remainder,dividend,divisor)
diff --git a/3808/CH4/EX4.3/Ex4_3.sce b/3808/CH4/EX4.3/Ex4_3.sce
new file mode 100644
index 000000000..e6c936256
--- /dev/null
+++ b/3808/CH4/EX4.3/Ex4_3.sce
@@ -0,0 +1,17 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+bin=[]
+n=input("Enter the length of the binary number:")
+dec=0
+disp("Enter the digits one by one")
+for i =1:n
+ bin(i)=input(" ")
+end
+for i=1:n
+ dec=dec*2+bin(i)
+end
+disp(dec,"Decimal Equivalent")
+
diff --git a/3808/CH4/EX4.4/Ex4_4.sce b/3808/CH4/EX4.4/Ex4_4.sce
new file mode 100644
index 000000000..ec7501830
--- /dev/null
+++ b/3808/CH4/EX4.4/Ex4_4.sce
@@ -0,0 +1,15 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+i=0
+oct=input("Enter the octal number:")
+tmp=oct
+dec=0
+while(oct~=0)
+ dec=dec+(modulo(oct,10))*(8**(i+0))
+ i=i+1
+ oct=int(oct/10)
+end
+disp(dec,'Equivalent Decimal Value:')
diff --git a/3808/CH4/EX4.5/Ex4_5.sce b/3808/CH4/EX4.5/Ex4_5.sce
new file mode 100644
index 000000000..787eb006c
--- /dev/null
+++ b/3808/CH4/EX4.5/Ex4_5.sce
@@ -0,0 +1,46 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+dec=[]
+d=0
+i=1
+disp('Please enter input in inverted commas')
+hex=input("Enter the hexadecimal number:")
+l=length(hex)
+hex=strsplit(hex)
+cn=0
+for i=l:-1:1
+ select hex(i)
+ case 'A' then
+ d=10
+ case 'B' then
+ d=11
+ case 'C' then
+ d=12
+ case 'D' then
+ d=13
+ case 'E' then
+ d=14
+ case 'F' then
+ d=15
+ case 'a' then
+ d=10
+ case 'b' then
+ d=11
+ case 'c' then
+ d=12
+ case 'd' then
+ d=13
+ case 'e' then
+ d=14
+ case 'f' then
+ d=15
+ else
+ d=eval(hex(i))
+ end
+ dec=dec+ (d) *(16**cn)
+ cn=cn+1
+end
+disp(dec)
diff --git a/3808/CH4/EX4.6/Ex4_6.sce b/3808/CH4/EX4.6/Ex4_6.sce
new file mode 100644
index 000000000..b21ef3a62
--- /dev/null
+++ b/3808/CH4/EX4.6/Ex4_6.sce
@@ -0,0 +1,17 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+arr=[]
+n=input("Enter the number:")
+tn=n
+while n~=0
+ re=pmodulo(n,8)
+ n=int(n/8)
+ arr($+1)=re
+end
+mprintf("The octal equivalent of the decimal number %d is:",tn)
+for i=length(arr):-1:1
+ mprintf("%d",arr(i))
+end
diff --git a/3808/CH4/EX4.7/Ex4_7.sce b/3808/CH4/EX4.7/Ex4_7.sce
new file mode 100644
index 000000000..bd1e6f5a1
--- /dev/null
+++ b/3808/CH4/EX4.7/Ex4_7.sce
@@ -0,0 +1,38 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+function dec_hex(num)
+rem=[]
+i=1
+len=0
+while num >0
+ rem(i)=pmodulo(num,16)
+ num=int(num/16)
+ i=i+1
+ len=len+1
+end
+disp("Hexadecimal Equivalent:")
+for i=len:-1:1
+ select rem(i)
+ case 10 then
+ disp('A')
+ case 11 then
+ disp('B')
+ case 12 then
+ disp('C')
+ case 13 then
+ disp('D')
+ case 14 then
+ disp('E')
+ case 15 then
+ disp('F')
+ else
+ disp(rem(i))
+end
+end
+endfunction
+
+inp=input("Enter the decimal number:")
+dec_hex(inp)
diff --git a/3808/CH4/EX4.8/Ex4_8.sce b/3808/CH4/EX4.8/Ex4_8.sce
new file mode 100644
index 000000000..63c5a0374
--- /dev/null
+++ b/3808/CH4/EX4.8/Ex4_8.sce
@@ -0,0 +1,25 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+bin_eq=[]
+decn=input("Enter the decimal number:")
+tn=decn
+i=1
+b=floor(decn/2)
+rem=modulo(decn,2)
+bin_eq(i)=string(rem(i))
+while 2<=b
+ decn=b
+ i=i+1
+ b=floor(decn/2)
+ rem=modulo(decn,2)
+ bin_eq(i)=string(rem)
+end
+bin_eq(i+1)=string(b)
+bin_eq=eval(bin_eq)
+mprintf("The binary equivalent of the decimal number %d is:",tn)
+for i=length(bin_eq):-1:1
+ mprintf("%d",bin_eq(i))
+end
diff --git a/3808/CH4/EX4.9/Ex4_9.sce b/3808/CH4/EX4.9/Ex4_9.sce
new file mode 100644
index 000000000..c961b295c
--- /dev/null
+++ b/3808/CH4/EX4.9/Ex4_9.sce
@@ -0,0 +1,25 @@
+//Chapter 04:Number Theory and Cryptography
+
+clc;
+clear all;
+
+bin_a=[]
+i=1
+rem=0
+n1=input("Enter 1st binary number:")
+n2=input("Enter 2nd binary number:")
+t1=n1
+t2=n2
+while (n1~=0 | n2~=0)
+ bin_a($+i)=modulo((modulo(n1,10)+modulo(n2,10)+rem),2)
+ rem=(modulo(n1,10)+modulo(n2,10)+rem)/2
+ n1=int(n1/10)
+ n2=int(n2/10)
+end
+if rem ~=0 then
+ bin_a($+i)=rem
+end
+bin_a=int(bin_a)
+bin_a=flipdim(bin_a,1)
+mprintf("The sum of binary numbers %d and %d is",t1,t2)
+disp(bin_a)