Euler Project No. 1 in R

Problem

Find the sum of all numbers below 1000 that can be divisible by 3 or 5

One of the solutions I created:

x <- c(1:999) values <- x[x %% 3 == 0 | x %% 5 == 0] sum(values 

The second solution I can’t work with and I need help. I pasted it below. I am trying to use a loop (here, I am using while (), and after that I will try ()). I am still afraid to keep references to indexes (locations in the vector) separate from values ​​/ observations inside vectors. The loops seem to make it more complicated for me to distinguish them.

Why does this not answer Euler # 1?

 x <- 0 i <- 1 while (i < 100) { if (i %% 3 == 0 | i %% 5 == 0) { x[i] <- c(x, i) } i <- i + 1 } sum(x) 

And in words, line by line, this is what I understand happening:

  • x gets the value 0
  • i get value 1
  • whereas the value of the object i (and not the C # index) is <1000
  • if divisible by 3 or 5
  • add that number i to the vector x
  • add 1 to i in order (to keep the loop within a certain limit of 1e3
  • sum all elements in vector x

I assume that x [i] <- c (x, i) is not the right way to add an element to the vector x. How to fix this and what else is inaccurate?

+7
for-loop r while-loop
source share
5 answers

First, your loop runs to i < 100 , not i < 1000 .

Secondly, replace x[i] <- c(x, i) with x <- c(x, i) to add an element to the vector.

+4
source share

Here is a shortcut that fulfills this amount, which is probably more in the spirit of the problem:

 3*(333*334/2) + 5*(199*200/2) - 15*(66*67/2) ## [1] 233168 

This is why it works:

The set of integers [1,999] contains:

333, which are divided by 3. Their sum is 3*sum(1:333) or 3*(333*334/2) .

199 values, which are divided by 5. Their sum is 5*sum(1:199) or 5*(199*200/2) .

Adding these values ​​gives a number that is too large at their intersection, which are values ​​that are divided by 15. There are 66 such values, and their sum is 15*(1:66) or 15*(66*67/2) 15*(1:66) 15*(66*67/2)

As a function of N, this can be written:

 f <- function(N) { threes <- floor(N/3) fives <- floor(N/5) fifteens <- floor(N/15) 3*(threes*(threes+1)/2) + 5*(fives*(fives+1)/2) - 15*(fifteens*(fifteens+1)/2) } 

Donation:

 f(999) ## [1] 233168 f(99) ## [1] 2318 
+3
source share

And one more way:

 x <- 1:999 sum(which(x%%5==0 | x%%3==0)) # [1] 233168 
+1
source share

A very effective approach is as follows:

 div_sum <- function(x, n) { # calculates the double of the sum of all integers from 1 to n # that are divisible by x max_num <- n %/% x (x * (max_num + 1) * max_num) } n <- 999 a <- 3 b <- 5 (div_sum(a, n) + div_sum(b, n) - div_sum(a * b, n)) / 2 

In contrast, a very short code looks like this:

 x=1:999 sum(x[!x%%3|!x%%5]) 
+1
source share

Here is an alternative that I think gives the same answer (using 99 instead of 999 as the upper bound):

 iters <- 100 x <- rep(0, iters-1) i <- 1 while (i < iters) { if (i %% 3 == 0 | i %% 5 == 0) { x[i] <- i } i <- i + 1 } sum(x) # [1] 2318 

Here is the for-loop mentioned in the original post:

 iters <- 99 x <- rep(0, iters) i <- 1 for (i in 1:iters) { if (i %% 3 == 0 | i %% 5 == 0) { x[i] <- i } i <- i + 1 } sum(x) # [1] 2318 
0
source share

All Articles