How to resolve the algorithm Prime decomposition step by step in the R programming language

Published on 12 May 2024 09:40 PM
#R

How to resolve the algorithm Prime decomposition step by step in the R programming language

Table of Contents

Problem Statement

The prime decomposition of a number is defined as a list of prime numbers which when all multiplied together, are equal to that number.

Write a function which returns an array or collection which contains the prime decomposition of a given number

n

{\displaystyle n}

greater than   1. If your language does not have an isPrime-like function available, you may assume that you have a function which determines whether a number is prime (note its name before your code). If you would like to test code from this task, you may use code from trial division or the Sieve of Eratosthenes. Note: The program must not be limited by the word size of your computer or some other artificial limit; it should work for any number regardless of size (ignoring the physical limits of RAM etc).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Prime decomposition step by step in the R programming language

Source code in the r programming language

findfactors <- function(num) {
  x <- NULL
  firstprime<- 2; secondprime <- 3; everyprime <- num
  while( everyprime != 1 ) {
    while( everyprime%%firstprime == 0 ) {
      x <- c(x, firstprime)
      everyprime <- floor(everyprime/ firstprime)
    }
    firstprime <- secondprime
    secondprime <- secondprime + 2
  }
  x
}

print(findfactors(1027*4))


primes <- as.integer(c())

max_prime_checker <- function(n){
  divisor <<- NULL

  primes <- primes[primes <= n]

  for(i in 1:length(primes)){
    if((n/primes[i]) %% 1 == 0){
      divisor[i]<<-1
    } else {
      divisor[i]<<-0
    } 
  }
  num_find <<- primes*as.integer(divisor)
  
  return(max(num_find))
}

#recursive prime finder
prime_factors <- function(n){
  
  factors <- NULL
  
  large <- max_prime_checker(n)
  n1 <- n/large 
  
  if(max_prime_checker(n1) == n1){
    factors <- c(large,n1)
    return(factors)
  } else {
    factors <- c(large, prime_factors(n1))
    return(factors)
  }
}


findfactors <- function(n) {
  a <- NULL
  if (n > 1) {
    while (n %% 2 == 0) {
      a <- c(a, 2)
      n <- n %/% 2
    }
    k <- 3
    while (k * k <= n) {
      while (n %% k == 0) {
        a <- c(a, k)
        n <- n %/% k
      }
      k <- k + 2
    }
    if (n > 1) a <- c(a, n)
  }
  a
}


  

You may also check:How to resolve the algorithm Koch curve step by step in the Forth programming language
You may also check:How to resolve the algorithm String append step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the Python programming language
You may also check:How to resolve the algorithm 9 billion names of God the integer step by step in the C programming language
You may also check:How to resolve the algorithm 24 game/Solve step by step in the Clojure programming language