Counting primes less than x arithmetically, Scilab, Matlab

// This programs counts the number of primes less than x without using the mod function or any if statements. It could perhaps be said that this is an arithmetic way of counting the primes because here we start with a single one and then sum, subtract, raise to powers, multiply and divide to produce the charachteristic function of the primes.
x=10;
T=zeros(x,x);

// Let the first element be equal to one
T(1,1)=1;
T;
for n=2:x;
for k=2:n;
s = 0;
for i=1:k-1;
//recursive definition of divisibility without using the mod function, table
s=s+T(n-i,k-1)-T(n-i,k);
end
T(n,k)=s;
end
end
T;

A=cumsum(T,'r');

A=inv(A);

B=zeros(x,x);
for n=1:x;
  for k=1:x;
    B(n,k)=(n/k)^(-A(n,k));
  end
end

V=prod(B,'c');

for n=1:x;
  W(n)=V(n)^(-A(n,1));
end

W(1)=0;
for n=2:x;
  W(n)=(W(n)-1)/(n-1);
end

//sequence A000720 in the OEIS, number of primes less than n (or less than x)
pi_n=cumsum(W,'r')
// Mats Granvik, mats.granvik (at) abo.fi
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Counting primes less than x arithmetically, Scilab, Matlab

  1. Mats Granvik says:

    Don’t take this piece of code above too seriously. I am not sure why I wrote it.

Comments are closed.