简体   繁体   中英

Project Euler Q #1

C# Project Euler

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.

When I run my code i enter in the first multiple "3" then my 2nd one "5" and then out off "1000" but i have come to a conclusion that my answer is exactly 1000 off from the real answer witch is "233168" my code is below and I am curious to see if anyone can see whats wrong.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("Find the sum of all the multiples of 3 or 5 below 1000.");
        Console.WriteLine("First Multiple: ");
        String Mult1 = Console.ReadLine();
        Console.WriteLine("Second Multiple:");
        String Mult2 = Console.ReadLine();
        Console.WriteLine("Out Of:");
        String outOfN = Console.ReadLine();
        int M1 = Int32.Parse(Mult1);
        int M2 = Int32.Parse(Mult2);
        int BN = Int32.Parse(outOfN);

        int MyResult1 = MyMathFunctions.FindMult(M1,M2, BN);
        
    
        Console.WriteLine("Your Answer is :" + MyResult1);
        Console.WriteLine("Answer should be: 233168");
        //Answer should be 233168


    }
    class MyMathFunctions
     {

        public static int FindMult(float M1,float M2, float BN)
        {
            int tot = 0;

            for(int i = 1 ; i <= BN; i++ )
                {
                   if( (i % M1 == 0) || (i % M2 == 0) )
                        {
                    tot += i;

                          }
           
                         }

                         return tot;
         }
      }
    }
 }

Read the problem more carefully.

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

No need to include the number "1000".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM