4

The following is guaranteed to return N consecutive composite numbers:

(N+1)!+2,(N+1)!+3........(N+1)!+(N+1) 

I used this to find 5 consecutive composite numbers in R using:

N=5
for(i in 2:6){a=factorial(N+1)+i;print(a);}
# [1] 722
# [1] 723
# [1] 724
# [1] 725
# [1] 726

However, I want first 'N' consecutive composite numbers, which this code is not guaranteed to return. For instance, for N=5, I want 24,25,26,27,28.

josliber
  • 41,865
  • 12
  • 88
  • 126
Aarthika
  • 81
  • 10
  • Are you looking for `factorial(N-1)+seq_len(N)-1`? – nicola Sep 10 '15 at 11:59
  • 1
    How are going to validate those are all composite numbers though? – David Arenburg Sep 10 '15 at 12:04
  • @nicola This command is true for N=5, But N=4 this is not useful. Because i got 6 7 8 9. Here 7 is not a composite number. – Aarthika Sep 10 '15 at 12:05
  • 2
    I really don't get the logic to the expected output with given input... – Tensibai Sep 10 '15 at 12:06
  • 1
    The `numbers` package has the `Primes` function that let you generate prime numbers. Then, you can easily find the first `N` sequence of composite numbers (if that's what you are looking for). – nicola Sep 10 '15 at 12:18
  • possible duplicate of http://stackoverflow.com/questions/32397169/how-to-find-consecutive-composite-numbers-in-r/32397742#32397742 – Maksim Gayduk Sep 10 '15 at 12:39

1 Answers1

4

You can generate a list of primes with numbers:::Primes or numbers:::primeSieve (thanks to @Nicola for pointing to this function in a comment!), compute the gaps between each with diff, and then return a sequence from the first prime number whose gap is at least N with seq:

library(numbers)
primes <- as.integer(numbers:::primeSieve(100000000))  # About 9 seconds
d <- diff(primes)
firstNComposite <- function(N) {
  valid <- which(d >= N+1)
  if (length(valid) == 0) {
    stop("Need to generate more prime numbers")
  } else {
    seq(primes[valid[1]]+1, length.out=N)
  }
}
firstNComposite(5)
# [1] 24 25 26 27 28
firstNComposite(200)
#   [1] 20831324 20831325 20831326 20831327 20831328 20831329 20831330 20831331 20831332 20831333 20831334
#  [12] 20831335 20831336 20831337 20831338 20831339 20831340 20831341 20831342 20831343 20831344 20831345
#  [23] 20831346 20831347 20831348 20831349 20831350 20831351 20831352 20831353 20831354 20831355 20831356
#  [34] 20831357 20831358 20831359 20831360 20831361 20831362 20831363 20831364 20831365 20831366 20831367
#  [45] 20831368 20831369 20831370 20831371 20831372 20831373 20831374 20831375 20831376 20831377 20831378
#  [56] 20831379 20831380 20831381 20831382 20831383 20831384 20831385 20831386 20831387 20831388 20831389
#  [67] 20831390 20831391 20831392 20831393 20831394 20831395 20831396 20831397 20831398 20831399 20831400
#  [78] 20831401 20831402 20831403 20831404 20831405 20831406 20831407 20831408 20831409 20831410 20831411
#  [89] 20831412 20831413 20831414 20831415 20831416 20831417 20831418 20831419 20831420 20831421 20831422
# [100] 20831423 20831424 20831425 20831426 20831427 20831428 20831429 20831430 20831431 20831432 20831433
# [111] 20831434 20831435 20831436 20831437 20831438 20831439 20831440 20831441 20831442 20831443 20831444
# [122] 20831445 20831446 20831447 20831448 20831449 20831450 20831451 20831452 20831453 20831454 20831455
# [133] 20831456 20831457 20831458 20831459 20831460 20831461 20831462 20831463 20831464 20831465 20831466
# [144] 20831467 20831468 20831469 20831470 20831471 20831472 20831473 20831474 20831475 20831476 20831477
# [155] 20831478 20831479 20831480 20831481 20831482 20831483 20831484 20831485 20831486 20831487 20831488
# [166] 20831489 20831490 20831491 20831492 20831493 20831494 20831495 20831496 20831497 20831498 20831499
# [177] 20831500 20831501 20831502 20831503 20831504 20831505 20831506 20831507 20831508 20831509 20831510
# [188] 20831511 20831512 20831513 20831514 20831515 20831516 20831517 20831518 20831519 20831520 20831521
# [199] 20831522 20831523
josliber
  • 41,865
  • 12
  • 88
  • 126
  • If i want more than that what i have to do? I got Error: cannot allocate vector of size 3.7 Gb error like this – Aarthika Sep 14 '15 at 10:23
  • @Aarthika you would need to use a different (more memory efficient) implementation of prime number generation than `numbers:::primeSieve`. – josliber Sep 14 '15 at 11:16
  • @Aarthika if you pass too big of an argument to `numbers:::primeSieve` then your computer will run out of memory. The only way to deal with this is to find a new way to generate prime numbers or to buy more memory. How big of problems are you trying to solve? – josliber Sep 14 '15 at 11:44
  • Okay...I need Atleast firstNComposite(500). New way to generate prime numbers means shall i have to find different command? – Aarthika Sep 14 '15 at 11:57
  • @Aarthika you'll need to find a new command -- sorry. I would particularly look for implementations of the segmented form of the Sieve of Eratosthenes, as it uses less memory than the non-segmented form. Unfortunately I don't know of any such implementation in R, and asking for library recommendations is off-topic on this site. You might try asking for software recommendations for implementations of the segmented sieve at http://softwarerecs.stackexchange.com/. Make sure to read their [posting guidelines](http://softwarerecs.stackexchange.com/help/on-topic) first, though. – josliber Sep 14 '15 at 16:58