How to check if a number is prime using a finite definite integral

b=0; p=input('Input a number : '); m=fix((p+1)/2); for k=2:m+1; fun=@(w) exp(p.*1j.*w).*exp(-2.*k.*1j.*w).*(1-exp(m.*k.*1j.*w))./(1-exp(-k.*1j.*w)); a=integral(fun,-pi,pi); b=b+a; end; b=b/(2*pi); disp(b); Above simple MATLAB/Octave program, can detect if a number is prime or not. If the result is zero (considering zero being less than 1e-5), the number introduced is a prime. If the result is an integer, this result will tell us how many permutations of two divisors, the input number has. As you can check, no recurrent division by odd or prime numbers is done. Just this strange integral: 1 2π ∫ e (∑ e ( 1 − e 1 − e−kjω ) k=m+1

Above simple MATLAB/Octave program, can detect if a number is prime or not.If the result is zero (considering zero being less than 1e-5), the number introduced is a prime.
If the result is an integer, this result will tell us how many permutations of two divisors, the input number has.As you can check, no recurrent division by odd or prime numbers is done.Just this strange integral: Being p the number that we want to check if it is a prime or not.And being m whatever integer number higher than +1 2 (the lowest, the most efficient the operation).As k and ω are independent variables, the sum can be taken outside the integral (as it is in the above program).
To get to this point, we will do the following.First, we will create a domain with all the composite numbers.This is easy, as you can just multiply one by one all the integers (greater or equal than 2) in that domain.So, you will get all the composite numbers (not getting any prime) in that domain.
Then, we will use the Fourier transform to change from this original domain (called discrete time domain in this regards) to the frequency domain.There, we can "ask", using Parseval's theorem, if a certain number is there or not.The use of Parseval's theorem leads to the above integral.If the number p that we want to check is not in the

Introduction
In this paper, we will obtain the following integral: )   − (1) Being p the number that we want to check if it is a prime or not.And being m whatever integer number higher than +1 2 .If the result is zero (or it is so small that it can be considered to be zero) the number p is a prime.If not, the result will tell us how many permutations of two divisors, the number p has To obtain the integral (1), the following steps shall be taken.First, we will create a domain with all the composite numbers in the domain included.
Then, we will use the Fourier transform [1] to change from the original domain (time domain) to the frequency domain.
Using the Parseval's theorem, we can check if a certain number is the above created domain or not.The use of Parseval's theorem will lead to the above integration.
If the number p that we want to check is not in the domain, the result of the integration is zero and the number is a prime.If instead, the result is an integer, this integer will tell us how many permutations of two divisors the number p has.And in consequence, how many divisors, the number has.

Matrix of composite numbers
It is very easy to create a matrix with the only composite numbers in certain domain.We can make that each element of the matrix is the result of the product of two integers in certain domain.We can perform this operation one by one until having used all the integers.So, the result will be all the composite numbers in certain domain. Example, And the result is: We can see that all the composite numbers between 4 and 10 are included in the matrix: 4,6,8,9,10.And as expected, the prime numbers 2,3,5 and 7 not.But we can see that the number 14 that is composite is not included.Why?Because it is composed by 7•2 and the 7 is not included in the multiplications.This means, the domain where it is valid that all the composite numbers are included (and none of the integers) finishes in 2•5=10 (being 5 the last number we are using in the multiplications).We can also see that it is a symmetric matrix, that will have implications in the following chapters.
We can increase the matrix to 7 for example and see the same effect: As 2•7=14, we know that the result will be reliable until 14.This means, all the composite numbers until 14 have to be included in the matrix: 4,6,8,9,10,12 and 14.
And not the primes: 2,3,5,7,11,13.We can see again that the composite number 22=11•2 is not included in the matrix.This is because 11 has not be included in the multiplications.The reliable domain is until 7•2=14, as commented.
The nomenclature that we will use in the following chapters for the general case will be the following: This means, the reliable domain (the domain where all the composite numbers will be included in the matrix) will be up to 2•(m+1).

Including the elements of the composite matrix in the discrete time domain
Now, the next movements will be quite straight forward.The first thing we have to do, is to introduce these calculated composite numbers in the discrete time domain.We will put unitary delta functions in the positions of the x axis indicated by the matrix of composite numbers: © 2018 Jesus Sanchez jesus.sanchez.bilbao@gmail.comORCID 0000-0002-5631-8195 https://www.researchgate.net/profile/Jesus_Sanchez645/21 We can see that in the above graphic, each Dirac delta represent the product of two integers, that compose a composite number.This means, each Dirac delta means that the composite number has a factorization of two numbers.And the order counts.This is, 2•4 counts one and 4•2 counts another one.
If we have only one Dirac Delta, it means that it has only one multiplication (therefore it is a square of a prime number) like 4=2•2 or 9=3•3 for example.If the number of Dirac deltas is odd, the number has an integer square root.If the number is even, it has different permutations of different products, but it does not have an integer square root.
We can create a function that is the sum of all these deltas.And this function will have the information of all these composite numbers.This function in the discrete domain is defined like this: This function f(n) has the information regarding all the composite numbers inside the domain 2•(m+1).But, the important information there is what it is not included.All the integers smaller than 2•(m+1) that are not included in this function, are prime numbers.
How do we spot them?If we want to know if the number 5 is prime or not.We have to check if there is a Dirac delta in position 5.This means, we have to check if the following function g(n) is inside the function f(n): © 2018 Jesus Sanchez jesus.sanchez.bilbao@gmail.comORCID 0000-0002-5631-8195 https://www.researchgate.net/profile/Jesus_Sanchez646/21 In general, if we want to check if whatever number p is prime, g(n) would be: We could do this performing the multiplication one by one for all the points in x axis between the function f(n) and the function g(n).
As the function g(n) is zero everywhere except in the point 5 (or in general, in p), if the result of the multiplication point by point between f(n) and g(n) is zero, it means that f(n) does not have a Dirac delta in 5. So, number 5 is prime.
If the result is different, it will tell us the value of f(n) at that point.And the value of f(n) at that point is the number of deltas included.This means, the number of all the possible multiplications of two integers that compose that number.
The problem is that to perform this operation in the time domain, does not add any value.Because to know the result, we have to compare "manually" f(n) and g(n) to see if f(n) has a value different from zero in the position where g(n) has the delta.This is the reason why we go to the next chapter.

The frequency domain
What we will do is to convert the functions f(n) an g(n) to the frequency domain.To do so, we use the Fourier transform.
We start with f(n).We know from signal theory that the Fourier transform of a shifted Dirac delta is: So, the function f(n), in the frequency domain has the form F: We can put above sum in the following form: © 2018 Jesus Sanchez jesus.sanchez.bilbao@gmail.comORCID 0000-0002-5631-8195 https://www.researchgate.net/profile/Jesus_Sanchez647/21 As you can see above, each line is a geometric series sum.So, using the known result of geometric series [2], we can write: For the function g, following the same process: The Fourier transform would be:

Parseval's theorem
Now, we get to the point.In the chapter 2, we wanted to perform the following operation: If the result is different from zero, it means that p is composite, as it means that g(n) : Has found another delta in the same position for the function f(n) defined before, that has only deltas in composite number positions.
This operation in the time domain does not have added value (as you have to perform all multiplications), but fortunately a guy called Marc-Antoine Parseval [3] will make us life easier.
© 2018 Jesus Sanchez jesus.sanchez.bilbao@gmail.comORCID 0000-0002-5631-8195 https://www.researchgate.net/profile/Jesus_Sanchez648/21 The general Parseval's theorem applied to discrete functions (as it is the case) says the following: The asterisk * means the complex conjugate.In all the functions we are dealing in this paper are real in the time domain so in the lefthand side no conjugation is necessary.
For the righthand side, yes, we have complex functions.But, as you can check all of them are exponential based.So, the conjugate is just to change the sign of the exponent.This is immediate from Euler's formula [4].
So, coming back, this means, we can perform the operation of multiplying point by point the two functions in the time domain, just making the definite integral form -π to π of the multiplication of the two functions (its Fourier Transform) in the frequency domain.
So, the integral in the frequency domain (that represents the multiplication point by point of the functions f(n) and g(n) in the time domain) according Parseval's theorem is the following (and we will call q to its result): So, summing up: So, this is the integral we have been talking about from the beginning.Now, you know where it comes from.We will call q the result of above formula.If q is zero, p is prime, © 2018 Jesus Sanchez jesus.sanchez.bilbao@gmail.comORCID 0000-0002-5631-8195 https://www.researchgate.net/profile/Jesus_Sanchez649/21 if q is different from zero, q has information regarding the number of factors.Let's comment about it in the following chapters.
Before that, just for possible calculation issues, it is to be noted that as, k is independent from ω the sum can be taken outside the integral, as follows:

Number of factors of p
We know that if p is composite, the number q (result from equation ()) is the number of possible combinations of multiplications of two integer numbers that get the value p.And yes, with this number we can get the number of factors that p has.This is the formula:  = ( 1 + 1)( 2 + 1)( 3 + 1) … (  + 1) − 2 (16) Being nf1 the number of times the factor f1 is repeated, nf2 the number of times f2 is repeated and so on.
Let's see with an example.We apply equation () to the number 20 and we get q=4.So we can check: This means, p has to have a factor that is repeated twice and one that appears only once.
And we see that it is true: The factor 2 is repeated twice and 5 once.
And the q is 4 because, there are four combinations of multiplications of two integers that get the result 20: This means, three factors that are not repeated, and in fact 102=2•3•17 Another one: p=36 q=7 This means two factors repeated twice (each).In fact, 36=2•2•3•3 You can see that the formula (17) works.We can use q to know how many factors a number p has if it is composite (or instead, detect it is a prime if q=0).
One of the problems, that this system has is that sometimes there are different solutions for the number of factors.For example for q=2, the typical answer is: This means, two different factors not repeated.For example, 21=3•7=7•3.
But it could be: For example, the number 8=2•4=4•2 It has q=2 (two combinations of factors) but 8=2•2•2 This means one factor three times, as seen before.
Regarding this point a very detailed study should be done.You can check in the Matlab program that I have reduced the precision of the integral to increase efficiency.I have reduced the precision of 0.24, which can be considered high, but it is sufficient for low values of p.
You can change and play with the value here: a=integral(fun,-pi,pi,'AbsTol',0.24); Also, I have considered implicitly that q is considered to be zero when q<0.5 (using the function "round".

Sum of the exponential of the factors of the number p
In the chapter 2 we have put all the Dirac deltas as unitary Dirac deltas.But we can introduce more information, if instead of putting unitary Dirac deltas, we introduce the deltas multiplied by a number that represents the value of the factors you are using.This means, as an output we can get info regarding the factors of the number p (not only the quantity of factors but its sum).We can check an example.We can multiply the delta by an exponential that represent the two factors we are using.In this case, it is the exponential of the sum of the two factors.We will see that these representation gives us more information and permits to follow the calculations in a similar manner as before.
© 2018 Jesus Sanchez jesus.sanchez.bilbao@gmail.comORCID 0000-0002-5631-8195 https://www.researchgate.net/profile/Jesus_Sanchez6414/21 So, in this case: To calculate Fourier transform: Calculating the geometric series result for each line: We call q2 this value that has this information regarding the factors.We will see how to use it in the next chapter.
One of the problems that this form has is that the exponentials e k and e m could go high very quick.A way to avoid this, is to use a "calibration factor" c.This factor, will reduce this exponentials value not reducing the information.This factor should be of the order of p, and normally using the value √p works well.This we will see later.First, the calculation (similar to above but with the calibration factor c included).This factor q3 is equivalent (has the same information) as q2 but reducing the exponentials value, so it reduces the computing necessities.
We see what to do with them, in the next chapter.

Special case: Discovering the factors of two-factor composite numbers
There are two cases where with all this information, it is immediate to calculate the factors of a number.One of the cases is trivial.When q=1, it means that p is a perfect square, so you can just make the square root to get it.
But there is another special case.When q=2, it means that the number is composite but only by two factors (two prime factors).This case is used for example in RSA encryption [5].
What you can do in this case is the following.
-First you calculate the q.And you see that is 2, that corresponds to (1+1)(1+1).This means, two different factors.So, you can apply the following.