I want Mathematica code the does what the following pseudocode does.
s = 0
for i = 1 to x
if i is a prime number then
s = s + 1, print s
else
print "unspecified"
I tried
numPrime1[n_] :=
Block[{i, j, g, s, f}, s = 0; f = 0; g = 0;
For[i = 1, i <= n, i++,
For[j = 1, j <= i, j++,
If[OddQ[i/j], f = f + 1]
]
If[f == 2, s = s + 1, break]
If[OddQ[n/i], g = g + 1;]
]
If[g == 2, Print[s], Print["Ungültig"];]]
numPrime1[17]
Now that just returns Null^3
Edit 1
Now it returns 1, but I don't know why it shouldn't be 1. Can someone give me advice to make that work?
Edit 2
Solved it now this way
numPrime1[n_] := Block[{i, j, g, s, f}, s = 0; f = 0; g = 0;
For[i = 1, i <= n, i++,
For[j = 1, j <= i, j++,
If[IntegerQ[i/j], f = f + 1];]
If[f == 2, s = s + 1];
If[IntegerQ[n/i], g = g + 1];
f = 0];
If[g == 2, Print[s], Print["Ungültig"]];]
numPrime1[17]
All my tests are now correct.
PrimePi
. Check details in documentation. $\endgroup$