# Primes in R (Part III): Schoolmath is Broken!

**The Pith of Performance**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Here we are in Part III. Wait!? What happened to Parts I and II? Well, I started to write an article about Amdahl’s law, parallelism and prime numbers, but found myself buried three levels deep trying to resolve problems with prime numbers in R. My normal inclination is to use Mathematica for such things, but I happened to already be using R for another reason so, I thought I’d see what it had to offer for calculating with primes. It now looks like that might have been a mistake.

**Putting primes in R**

The first problem is that prime functions are not installed with R base packages by default (because they’re not often used in statistical calculations, I suppose) so, you have to find and install a package that has them. Googling around, I landed on primeFactors in the **R.basic** package, but that failed to install. Grrr!

**Added Note:** On Mon, Jun 14, 2010, David Smith (VP of Marketing, Revolution Analytics) pointed me at the GMP package on CRAN. Although it does have an isprime() tester, there is no prime number generator. Also, since `isprime` returns a 2, 1 or 0 depending on whether the number is, might be, or is not a prime, it’s probably easier to use it with an `ifelse` function:

<br />> isprime(29)<br />[1] 2<br />> ifelse(isprime(29)==2,"yes","no")<br />[1] "yes"<br />

Undaunted, I finally found the package called

**schoolmath**, mentioned over at R-bloggers, and it appeared to have just the functions I needed. For example, it has a convenient function called

*primes*that lists all the prime numbers within a specified range.

Good. Let’s try listing the primes between 2 and 10:

<br />> library(schoolmath)<br />> primes(2,10)<br />[1] 1 2 3 5 7 11 13 17<br />Huh!? That’s not right. It turns out that this result occurs because the underlying R code says:

<br />if (start < 18) { primzahlen <- c(1, 2, 3, 5, 7, 11, 13, 17) <br />which is bloody annoying! The other glaring problem is the 1. Who ordered that? (see Muon) The number 1 was once considered prime, but not these days. The usage note states:

`primes(start = 12, end = 9999)`. Sigh!

**Counting primes**

Ignoring that strangeness (I can always decrement by 1), it appears to do better if you ask for a bigger range. Let’s look at primes less than 500:

<br />> rsm500 <- primes(2,500)<br />> rsm500<br /> [1] 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71<br />[22] 73 79 83 89 97 101 103 107 109 113 127 131 133 137 139 149 151 157 163 167 173<br />[43] 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 259 263 269 271 277 281<br />[64] 283 293 301 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409<br />[85] 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499<br />>length(rsm500)<br />[1] 99<br />Hang on, that’s wrong too! There are only 95 primes less than 500. You can check against this correct list on the web, and here they are from Mathematica:

<br />> mma500<br /> [1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73<br />[22] 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181<br />[43] 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307<br />[64] 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433<br />[85] 439 443 449 457 461 463 467 479 487 491 499<br />> length(mma500)<br />[1] 95<br />Now, I have to find out where the inconsistencies reside.

**Debugging the count**

To find the wrong additional entries, I wrote an R script and here’s the output. The first column is just the row number, which starts at zero (because of the incorrect inclusion of 1 in the R package) so that successive entries line up. The second column (MMA) contains the Mathematica primes and the third column (Rpkg) is the primes produced by the **schoolmath** package.

<br />Comparison with Mathematica list of primes<br /> MMA Rpkg<br /> 0 NaN 1 *<br /> 1 2 2 <br /> 2 3 3 <br /> 3 5 5 <br /> 4 7 7 <br /> 5 11 11 <br /> 6 13 13 <br /> 7 17 17 <br /> 8 19 19 <br /> 9 23 23 <br />10 29 29 <br />11 31 31 <br />12 37 37 <br />13 41 41 <br />14 43 43 <br />15 47 47 <br />16 53 53 <br />17 59 59 <br />18 61 61 <br />19 67 67 <br />20 71 71 <br />21 73 73 <br />22 79 79 <br />23 83 83 <br />24 89 89 <br />25 97 97 <br />26 101 101 <br />27 103 103 <br />28 107 107 <br />29 109 109 <br />30 113 113 <br />31 127 127 <br />32 131 131 <br />33 137 133 *<br />34 139 137 *<br />35 149 139 *<br />36 151 149 *<br />37 157 151 *<br />38 163 157 *<br />39 167 163 *<br />40 173 167 *<br />41 179 173 *<br />42 181 179 *<br />43 191 181 *<br />44 193 191 *<br />45 197 193 *<br />46 199 197 *<br />47 211 199 *<br />48 223 211 *<br />49 227 223 *<br />50 229 227 *<br />51 233 229 *<br />52 239 233 *<br />53 241 239 *<br />54 251 241 *<br />55 257 251 *<br />56 263 257 *<br />57 269 259 *<br />58 271 263 *<br />59 277 269 *<br />60 281 271 *<br />61 283 277 *<br />62 293 281 *<br />63 307 283 *<br />64 311 293 *<br />65 313 301 *<br />66 317 307 *<br />67 331 311 *<br />68 337 313 *<br />69 347 317 *<br />70 349 331 *<br />71 353 337 *<br />72 359 347 *<br />73 367 349 *<br />74 373 353 *<br />75 379 359 *<br />76 383 367 *<br />77 389 373 *<br />78 397 379 *<br />79 401 383 *<br />80 409 389 *<br />81 419 397 *<br />82 421 401 *<br />83 431 409 *<br />84 433 419 *<br />85 439 421 *<br />86 443 431 *<br />87 449 433 *<br />88 457 439 *<br />89 461 443 *<br />90 463 449 *<br />91 467 457 *<br />92 479 461 *<br />93 487 463 *<br />94 491 467 *<br />95 499 479 *<br />96 NaN 487 *<br />97 NaN 491 *<br />98 NaN 499 *<br /><br />Bad R pkg values: 1 133 259 301<br />The end of this output (scroll down) shows 4 incorrectly included composite numbers (integers that are not prime).

In row 0, Mathematica (correctly) does not produce 1 as a prime, so its value is shown as NaN and an asterisk is appended to that row to indicate a discrepancy. Rows 1 through 32 are consistent with each other. The trouble starts at line 33 where R incorrectly inserts 133 as a prime in its list. We can easily check that 133 is not prime using the **schoolmath** function `is.prim()`:

<br />> is.prim(133)<br />[1] FALSE<br />With 4 composites wrongly listed as primes you get a count of 99 instead of the correct 95.

This is rather troubling. My experience with R is that it has surprisingly few bugs. This **schoolmath** package is the worst case of bugs I’ve seen on CRAN. Perhaps I should have been warned by the name of the package, but it was recommended by a mathematician. **Schoolmath** needs to go back to school. I hope the authors will enroll it and soon.

**leave a comment**for the author, please follow the link and comment on their blog:

**The Pith of Performance**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.