GORITHM - Finding the Sum of Multiples of 3 and 5 in C#

  • Context: C# 
  • Thread starter Thread starter rollcast
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a coding problem related to finding the sum of all multiples of 3 or 5 below 1000 using C#. Participants share code snippets and seek feedback on their implementations, focusing on the correct use of control flow statements in the context of the problem.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a code snippet intended to calculate the sum of multiples of 3 and 5 below 1000, questioning its correctness.
  • Another participant asks about the purpose of the "continue" statement in the provided code.
  • Clarifications are made regarding the function of the "continue" statement, with one participant noting that it skips the rest of the loop and starts the next iteration.
  • Concerns are raised about the use of "continue" in the context of avoiding double counting for numbers that are multiples of both 3 and 5.
  • A revised code snippet is shared that simplifies the logic by combining the conditions for multiples of 3 and 5, leading to a positive response regarding its correctness.

Areas of Agreement / Disagreement

Participants express differing views on the use of the "continue" statement, with some clarifying its function and others questioning its necessity in the original code. The discussion remains unresolved regarding the optimal approach to avoid double counting.

Contextual Notes

The discussion highlights potential misunderstandings about control flow in C#, particularly the implications of using "continue" within loops. There are also unresolved aspects regarding the handling of numbers that are multiples of both 3 and 5.

rollcast
Messages
403
Reaction score
0
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

I'm new to C# and I'm trying to solve this problem but there's no solution availible on Project Euler for it. I have made this bit of code and the result seems ok.

Code:
using System;

class MultipleSum
{
    public static void Main()
    {
        int sum = 0;

        for (int i = 0; i < 1000; i++)
        {
            if (i % 3 == 0)
                sum += i;
            continue;

            if (i % 5 == 0)
                sum += i;
            continue;
        }

        Console.WriteLine(sum);
        Console.WriteLine("Press ANY Key to continue");
        Console.ReadKey();
    }
}

Will that work for the problem?

Thanks
AL
 
Technology news on Phys.org
Hi rollcast! :smile:


rollcast said:
I'm new to C# and I'm trying to solve this problem but there's no solution availible on Project Euler for it. I have made this bit of code and the result seems ok.

Code:
using System;

class MultipleSum
{
    public static void Main()
    {
        int sum = 0;

        for (int i = 0; i < 1000; i++)
        {
            if (i % 3 == 0)
                sum += i;
            continue;

            if (i % 5 == 0)
                sum += i;
            continue;
        }

        Console.WriteLine(sum);
        Console.WriteLine("Press ANY Key to continue");
        Console.ReadKey();
    }
}

Will that work for the problem?

Thanks
AL

What is your purpose for the use of "continue" in your program?
 
I thought I needed them to continue the loop after I performed the += operator?
 
If you "continue" the loop, the rest of the loop is skipped and the next iteration starts.
Was that your intention?

Btw, the second "continue" is at the end of the loop and has as such no function.
 
rollcast said:
I thought I needed them to continue the loop after I performed the += operator?

Either you are using English in a fairly loose way, or you need to find out what the continue statement really does.

FWIW your code "works" because of the first continue statement, but the second one does nothing. But you wouldn't normally use a continue statement the way you used it.

http://www.cplusplus.com/doc/tutorial/control/
 
I was using the continue statements so in the event of a number, eg 60, that is both a multiple of 3 and 5 to stop it getting added twice? Or did I misinterpret the problem?
 
You interpreted the problem correctly.
However, as it is now, you *always* continue, whether the number is a multiple of 3 or not.
 
Code:
using System;

class MultipleSum
{
    public static void Main()
    {
        int sum = 0;

        for (int i = 3; i < 1000; i++)
        {
            if (i % 3 == 0 || i % 5 == 0)
            {
                sum += i;
            }

        }

        Console.WriteLine(sum.ToString());
        Console.WriteLine("Press ANY Key to continue");
        Console.ReadKey();
    }
}

Is this code more correct then?
 
Yep! :smile:
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K