Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8409279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:51:52+00:00 2026-06-09T23:51:52+00:00

This is quite literally the first problem in Project Euler. I created these two

  • 0

This is quite literally the first problem in Project Euler. I created these two algorithms to solve it, but they each yield different answers. Basically, the job is to write a program that sums all the products of 3 and 5 that are under 1000.

Here is the correct one:

divisors<-0


for (i in 1:999){
    if ((i %% 3 == 0) || (i %% 5 == 0)){
        divisors <- divisors+i 
        }
    }

The answer it yields is 233168

Here is the wrong one:

divisors<-0

for (i in 1:999){
    if (i %% 3 == 0){
        divisors <- divisors + i
        }
    if (i %% 5 == 0){
        divisors <- divisors + i 
        }
    }

This gives the answer 266333

Can anyone tell me why these two give different answers? The first is correct, and obviously the simpler solution. But I want to know why the second one isn’t correct.

EDIT: fudged the second answer on accident.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:51:53+00:00Added an answer on June 9, 2026 at 11:51 pm

    Because multiples of 15 will add i once in the first code sample and twice in the second code sample. Multiples of 15 are multiples of both 3 and 5.

    To make them functionally identical, the second would have to be something like:

    divisors<-0
    for (i in 1:999) {
        if (i %% 3 == 0) {
            divisors <- divisors + i
        } else {
            if (i %% 5 == 0) {
                divisors <- divisors + i 
            }
        }
    }
    

    But, to be honest, your first sample seems far more logical to me.

    As an aside (and moot now that you’ve edited it), I’m also guessing that your second output value of 26633 is a typo. Unless R wraps integers around at some point, I’d expect it to be more than the first example (such as the value 266333 which I get from a similar C program, so I’m assuming you accidentally left of a 3).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this quite popular problem, but have failed to find a solution that
This is quite weird but my search function can search for any word except
So I've literally been stuck on this for hours and it's quite frustrating. I
Microsoft's Concurrency and Coordination Runtime quite literally saved a project that was running into
I mean this quite literally. A close relative wants to create her own website
This is quite literally driving me bananas - I'm on holiday as from tomorrow
Well, this is quite the interesting problem. I've laid out an NSTableView (view-based) which
I discovered this quite by accident while looking for a file with a number
I've edited this quite a bit and bolded my question at this point. I
Why does this quite simple xquery takes 10min to execute in sql server (the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.