Learn R Programming

cwhmisc (version 5.0)

factor: Create primes, factor an integer, combine factors, check if prime

Description

Create primes, determine the prime factors of an integer together with their multiplicities, recombine factors, primitive version of the sieve of Eratosthenes.

Usage

primes(n)
Eratosthenes(n)
factorN(n)
prodN(fp)
is.prime(n)

Arguments

n
positive integer, number of primes, number to be factored, to be tested
fp
2-colummn matrix with prime factors and multiplicities

Value

  • primesGenerate the first n primes, also found in PRIMES.
  • EratosthenesExecute the sieve of Eratosthenes.
  • factorNDetermine the prime factors together with their multiplicities.
  • prodNRecombine factors, inverse of factorN.
  • is.primeCheck if positive integer is prime.
  • PRIMESThe first primes up to 17389.

Examples

Run this code
(p <- factorN( 423))
## [1,]   3 47
## [2,]   2  1
# meaning 423 = 3^2 * 47^1
prodN(p) # 423
is.prime(.Machine$integer.max) # TRUE
## check speed of your machine
  s <- Sys.time(); p<-primes(10^4);difftime(Sys.time(),s) 
## Time difference of 8.36586 secs  on my machine  
x <-factorN(.Machine$integer.max)

Run the code above in your browser using DataLab