Type Numbers
by P.H. Stikker
Introduction
This Maple worksheet includes 13 small procedures that check what type of number a given number or set of numbers is/are. The procedures itself are in the section "Procedures".
In the section "what is a.." is an explanation of what these numbers are.
In the section "Examples/Explanation" are examples of each procedure.
The section "package" helps you to create a package that will include all 13 procedures.
Some of these procedures (such as divisors and mersenne) are already built into Maple but work differently from the implementation here. For more information on Maple's number theory procedures, check the online help.
Procedures
Perfect
>
perfect:= proc(i,m)
local j,k,l,p,q:
for j from i to m do
p:=0;
for l from 1 to (0.5*j) do
k:=j/l;
if type(k,integer)=true then
p:=p+l;
fi:
od:
q:=p;
if q=j then
print(q,"Perfect Number");
fi:
od:
end:
Abundant
>
abundant:= proc(i,m)
local j,k,l,p,q:
for j from i to m do
p:=0;
for l from 1 to (0.5*j) do
k:=j/l;
if type(k,integer)=true then
p:=p+l;
fi:
od:
q:=p;
if q>j then
print(j,"Abundant Number");
fi:
od:
end:
Deficient
>
deficient:= proc(i,m)
local j,k,l,p,q:
for j from i to m do
p:=0;
for l from 1 to (0.5*j) do
k:=j/l;
if type(k,integer)=true then
p:=p+l;
fi:
od:
q:=p;
if q<j then
print(j,"Deficient Number");
fi:
od:
end:
Amicable
>
Amicable:=proc(a,b)
local p,j,k,q,i,h,r:
p:=0;
for j from 1 to (0.5*a) do
k:=a/j:
if type(k,integer)=true then
p:=p+j:
fi:
h:=p:
od:
if h=b then
q:=0;
p:=0;
for j from 1 to (0.5*b) do
k:=b/j:
if type(k,integer)=true then
p:=p+j:
fi:
q:=p:
od:
fi:
if q=a then
print(a,b,"are Amicable");
else
print(a,b,"are not Amicable");
fi:
end:
TypeNumber
>
TypeNumber:=proc(m)
deficient(m,m);
abundant(m,m);
perfect(m,m);
end:
ListType
>
ListType:=proc(i,m)
local j,k,l,p,q:
for j from i to m do
p:=0;
for l from 1 to (0.5*j) do
k:=j/l;
if type(k,integer)=true then
p:=p+l;
fi:
od:
q:=p;
if q=j then
print(j,"Perfect Number");
fi:
if q>j then
print(j,"Abundant Number");
fi:
if q<j then
print(j,"Deficient Number");
fi:
od:
end:
ProperDivisors
>
ProperDivisors:=proc(a)
local j,k,p:
p:=0;
for j from 1 to (0.5*a) do
k:=a/j:
if type(k,integer)=true then
print(j);
fi:
od:
end:
Divisors
>
Divisors:=proc(a)
local j,k,p:
p:=0;
for j from 1 to (0.5*a) do
k:=a/j:
if type(k,integer)=true then
print(j);
fi:
od:
print(a);
end:
Sophie
>
Sophie:=proc(a)
if isprime(a)=true then
if isprime(2*a+1)=true then
print(a,"is a Sophie Germain prime");
else
print(a,"is prime but not a Sophie Germain prime");
fi:
else
print(a,"is not prime");
fi:
end:
RNRPrime
>
RNRPrime:=proc(a)
local i,g:
g:=0;
if isprime(a)=false
then print(a,"is not prime");
else for i from 1 to (a-3)/2
do if type(numer(bernoulli(i*2))/a,integer)=true
then g:=g+1;
fi:
od:
if g>0
then print(a, "is a non regular prime");
else print(a, "is a regular prime");
fi:
fi:
end:
Mersenne
>
Mersenne:=proc(a)
local x,h,i,j:
h:=a+1;
i:=solve(2^x=h);
j:=simplify(i);
if type(j,integer)=true then print(a,"is a Mersenne Prime"); else print(a, "is not a Mersenne Prime"); fi:
end:
TypePrime
>
TypePrime:=proc(a)
if isprime(a)=true then
Sophie(a);
RNRPrime(a);
Mersenne(a);
else
print(a," is not prime");
fi:
end:
>
ListTypePrime
>
ListTypePrime:=proc(a,b)
local i:
for i from a to b do
TypePrime(i);
od:
end:
Cullen number
>
Cullen:=proc(a)
print(a*2^a+1);
end:
Cullen Prime
>
CullenPrime:=proc(a)
if isprime(a*2^a+1)=true then print(C[a]," is Cullen Prime");
fi:
end:
Fibonacci Prime
>
FibPrime:=proc(a)
with(combinat, fibonacci):
if isprime(fibonacci(a))=true then print(F[a], "is a Fibonacci Prime");
else (F[a]," is not prime");
fi:
end:
Fermat Prime
>
FermatPrime:=proc(a)
with(numtheory,fermat):
if isprime(fermat(a))=true then print(F[a]," is a Fermat Prime");
else
print(F[a]," is not Prime");
fi:
end:
Generalized Fermat Number
>
GFN:=proc(b,n)
print(F[b,n] = (b^2)^n+1);
end:
Generalized Fermat Prime
>
GFP:=proc(b,n)
if isprime((b^2)^n+1)=true then print(F[b,n] = (b^2)^n+1, "is a Generalized Fermat Prime");
else print(F[b,n] = (b^2)^n+1, "is not a Generalized Fermat Prime");
fi:
end:
NSW number
>
NSW:=proc(m)
print(S[2*m+1] = round(evalf(((1 + sqrt(2))^(2*m+1) + (1 - sqrt(2))^(2*m+1))/2)));
end:
NSW Prime
>
NSWPrime:=proc(m)
if isprime(round(evalf(((1 + sqrt(2))^(2*m+1) + (1 - sqrt(2))^(2*m+1))/2)))=true then print(S[2*m+1] = round(evalf(((1 + sqrt(2))^(2*m+1) + (1 - sqrt(2))^(2*m+1))/2))," is a NSW Prime");
else
print(S[2*m+1] = round(evalf(((1 + sqrt(2))^(2*m+1) + (1 - sqrt(2))^(2*m+1))/2))," is not a NSW Prime");
fi:
end:
what is a..
Perfect number
A number is perfect if it is equal to the sum of its proper divisors. 6 is perfect, because 1 + 2 + 3 = 6.
Abundant
A number is abundant if the sum of its proper divisors is greater than the number itself. For example, the proper divisors of 24 are {1, 2, 3, 4, 6, 8, 12} and 1 + 2 + 3 + 4 + 6 + 8 + 12 = 36, so 24 is abundant.
Deficient
If the sum of a number's proper divisors is less than the original number, it is called a deficient number. For instance, 16 is deficient. The proper divisors of 16 are {1, 2, 4, 8}, but 1 + 2 + 4 + 8 = 15.
Amicable pair
You might think of an amicable pair as two numbers that are best friends. The sum of the proper divisors of the first number is the second number, and if you add up the proper divisors of the second number, you get the first number. Here's an example. One amicable pair is 2620 and 2924. The proper divisors of 2620 are {1, 2, 4, 5, 10, 20, 131, 262, 524, 655, 1310}. Their sum is 1 + 2 + 4 + 5 + 10 + 20 + 131 + 262 + 524 + 655 + 1310 = 2924. Next we check whether 2924's proper divisors add up to 2620. 2924's proper divisors are {1, 2, 4, 17, 34, 43, 68, 86, 172, 731, 1462}. 1 + 2 + 4 + 17 + 34 + 43 + 68 + 86 + 172 + 731 + 1462 = 2620, so the pair of numbers really is amicable.
Proper Divisor
see first "divisors"
Proper divisors are the divisors less than the integer you started with: the proper divisors of 6 are {1, 2, 3}.
Divisor
The numbers that divide evenly into an integer are called its divisors. For example, the divisors of 6 are {1, 2, 3, 6}.
Sophie Germain prime
If both a number (n) and 2n + 1 are prime, then n is a Sophie Germain Prime.
Regular/irregular Prime
a prime is regular if it does not divide the class number of the algebraic number field defined by adjoining a pth root of unity to the rationals. If a prime is not regular, we call it irregular.
Mersenne Prime
A Mersenne number 2^n-1 which is prime is called a Mersenne prime. If m divides n, then 2^m-1 divides 2^n-1.
(Mersenne numbers are integers of the form Mn=2^n-1)
Examples/Explanation
perfect(a,b) checks if there are any perfect numbers between a and b:
> perfect(1,100);
abundant(a,b) checks if there are any abundant numbers between a and b:
> abundant(23,24);
deficient(a,b) checks if there are any deficient numbers between a and b:
> deficient(16,17);
TypeNumber(a) checks wether a is perfect, abundant or deficient:
> TypeNumber(16);
ProperDivisors(a) gives all the proper divisors of a:
> ProperDivisors(6);
Divisors(a) gives all divisors of a:
> Divisors(6);
Amicable(a,b) checks wether a and b are amicable:
> Amicable(2620,2924);
> Amicable(657,568);
ListType(a,b) gives the type of number (perfect, deficient or abundant) for all numbers from a to b:
> ListType(1,15);
Sophie checks if a given number is a Sophie Germain Prime
> Sophie(3);
> Sophie(4);
> Sophie(7);
RNRPrime checks if a given number is prime, a regular prime or a non regular prime
> RNRPrime(37);
> RNRPrime(13);
> RNRPrime(8);
Mersenne checks if a given number is a Mersenne prime
> Mersenne(13);
> Mersenne(7);
TypePrime checks if a given number is a Sophie Germain, Mersenne, regular or/and non regular prime
> TypePrime(3);
ListTypePrime gives you an opportunity to get information on a list of numbers:
> ListTypePrime(1,21);
Package
To make a package out of all the procedures create a directory "c:\MyLib\" and put the following in a [> format and than run it:
TypeNumber:=module()
export perfect, abundant, deficient, Amicable, TypeNumber, ListType, ProperDivisors, Divisors, Sophie, RNRPrime, Mersenne, TypePrime, ListTypePrime;
option package;
perfect:= proc(i,m)
local j,k,l,p,q:
for j from i to m do
p:=0;
for l from 1 to (0.5*j) do
k:=j/l;
if type(k,integer)=true then
p:=p+l;
fi:
od:
q:=p;
if q=j then
print(q,"Perfect Number");
fi:
od:
end:
abundant:= proc(i,m)
local j,k,l,p,q:
for j from i to m do
p:=0;
for l from 1 to (0.5*j) do
k:=j/l;
if type(k,integer)=true then
p:=p+l;
fi:
od:
q:=p;
if q>j then
print(j,"Abundant Number");
fi:
od:
end:
deficient:= proc(i,m)
local j,k,l,p,q:
for j from i to m do
p:=0;
for l from 1 to (0.5*j) do
k:=j/l;
if type(k,integer)=true then
p:=p+l;
fi:
od:
q:=p;
if q<j then
print(j,"Deficient Number");
fi:
od:
end:
Amicable:=proc(a,b)
local p,j,k,q,i,h,r:
p:=0;
for j from 1 to (0.5*a) do
k:=a/j:
if type(k,integer)=true then
p:=p+j:
fi:
h:=p:
od:
if h=b then
q:=0;
p:=0;
for j from 1 to (0.5*b) do
k:=b/j:
if type(k,integer)=true then
p:=p+j:
fi:
q:=p:
od:
fi:
if q=a then
print(a,b,"are Amicable");
else
print(a,b,"are not Amicable");
fi:
end:
TypeNumber:=proc(m)
deficient(m,m);
abundant(m,m);
perfect(m,m);
end:
ListType:=proc(i,m)
local j,k,l,p,q:
for j from i to m do
p:=0;
for l from 1 to (0.5*j) do
k:=j/l;
if type(k,integer)=true then
p:=p+l;
fi:
od:
q:=p;
if q=j then
print(j,"Perfect Number");
fi:
if q>j then
print(j,"Abundant Number");
fi:
if q<j then
print(j,"Deficient Number");
fi:
od:
end:
ProperDivisors:=proc(a)
local j,k,p:
p:=0;
for j from 1 to (0.5*a) do
k:=a/j:
if type(k,integer)=true then
print(j);
fi:
od:
end:
Divisors:=proc(a)
local j,k,p:
p:=0;
for j from 1 to (0.5*a) do
k:=a/j:
if type(k,integer)=true then
print(j);
fi:
od:
print(a);
end:
Sophie:=proc(a)
if isprime(a)=true then
if isprime(2*a+1)=true then
print(a,"is a Sophie Germain prime");
else
print(a,"is prime but not a Sophie Germain prime");
fi:
else
print(a,"is not prime");
fi:
end:
RNRPrime:=proc(a)
local i,g:
g:=0;
if isprime(a)=false
then print(a,"is not prime");
else for i from 1 to (a-3)/2
do if type(numer(bernoulli(i*2))/a,integer)=true
then g:=g+1;
fi:
od:
if g>0
then print(a, "is a non regular prime");
else print(a, "is a regular prime");
fi:
fi:
end:
Mersenne:=proc(a)
local x,h,i,j:
h:=a+1;
i:=solve(2^x=h);
j:=simplify(i);
if type(j,integer)=true then print(a,"is a Mersenne Prime"); else print(a, "is not a Mersenne Prime"); fi:
end:
TypePrime:=proc(a)
if isprime(a)=true then
Sophie(a);
RNRPrime(a);
Mersenne(a);
else
print(a," is not prime");
fi:
end:
ListTypePrime:=proc(a,b)
local i:
for i from a to b do
TypePrime(i);
od:
end:
end module:
> #libname:="c:/MyLib/TypeNumber",libname;
> #march('create',libname[1],100);
> #savelib('TypeNumber');