How to resolve the algorithm Factors of an integer step by step in the R programming language

Published on 12 May 2024 09:40 PM
#R

How to resolve the algorithm Factors of an integer step by step in the R programming language

Table of Contents

Problem Statement

Compute the   factors   of a positive integer. These factors are the positive integers by which the number being factored can be divided to yield a positive integer result. (Though the concepts function correctly for zero and negative integers, the set of factors of zero has countably infinite members, and the factors of negative integers can be obtained from the factors of related positive numbers without difficulty;   this task does not require handling of either of these cases). Note that every prime number has two factors:   1   and itself.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Factors of an integer step by step in the R programming language

Source code in the r programming language

factors <- function(n)
{
   if(length(n) > 1) 
   {
      lapply(as.list(n), factors)
   } else
   {
      one.to.n <- seq_len(n)
      one.to.n[(n %% one.to.n) == 0]
   }
}

factors <- function(n) c(Filter(function(x) n %% x == 0, seq_len(n %/% 2)), n)
#Vectorize is an interesting alternative to the previous solution's lapply.
manyFactors <- function(vec) Vectorize(factors)(vec)

  

You may also check:How to resolve the algorithm Emirp primes step by step in the Tcl programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the Prolog programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the Maple programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the C# programming language
You may also check:How to resolve the algorithm Array length step by step in the zkl programming language