## Problem 1 (first method) x <- 6 # x is our input positive integer for (i in 1:(x-1) ) # i will run from 1 to x-1 (1 to 5) { if ( x %% i == 0 ) # will be true when i is a factor { print(i) } } ## Problem 1 (second method) x <- 6 factors <-NULL for (i in 1:floor(x/2) ) { if ( x %% i == 0 ) { factors <- c(factors, i) } } factors # prints the vector of factors ## Problem 2 x <- 6 f <- 0 factors <-NULL for (i in 1:(x-1) ) { if ( x %% i == 0 ) { factors <- c(factors, i) } } if (sum(factors) == x ) { f=1 } f ## Problem 3 perfectNumbers <- NULL for (x in 2:10000) { factors <-NULL f <- 0 for (i in 1:floor(x/2) ) { if ( x %% i == 0 ) { factors <- c(factors, i) } } if (sum(factors) == x ) { f <- 1 } if ( f ) { perfectNumbers <- c(perfectNumbers, x) } } perfectNumbers ## Problem 4 x <- 256 floor((x %% 100)/10)