Archive for June, 2011

Find the sum of all the multiples of 3 or 5 below or equal to 1000

To find the sum of all the multiples of 3 or 5 below or equal to 1000, first we have to find the sum of all the numbers which are divisible by 3, than sum of numbers which are divisible by 5. Once we are done with this we have to subtract the sum of numbers which are divisible by 3 and 5.

There are two approaches to solve this problem.

  1. The first approach is to write a loop and find the numbers which are divisible by 3,5 and add them, after this subtract the numbers which are divisible by 3 and 5.
  2. The other approach is to find the count of numbers which are divisible by 3,5 and 15. After this apply the arithmetic progression’s sum formula to find the sum.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
  class Program
  {
    static void Main(string[] args)
    {
      int sum = 0;
      int range = 1000;
      int Number1 = 3; int Number2 = 5;
      for (int i = 0, j = 0; i <= range; i += Number1, j += Number2)
      {
        sum += i;
        if (j < range && j % Number2 == 0)
        {
          sum += j;
        }
        if (i <= range && i % Number2 * Number1 == 0)
        {
          sum -= i;
        }

      }
      Console.WriteLine("\n");
      Console.WriteLine("Sum of all the multiples of 3 or 5 below or equal to 1000" +
      "using Loop "  + sum);

      int Total_Number1 = range / Number1;
      int Total_Number2 = range / Number2;
      int Total_Number1AndNumber2 = range / (Number1 * Number2);

      int sumTotal_Number1 = Total_Number1 * (Number1 + Total_Number1 * Number1) / 2;
      int sumTotal_Number2 = Total_Number2 * (Number2 + Total_Number2 * Number2) / 2;
      int sumTotal_Number1andNumber2 = Total_Number1AndNumber2 * (Number2 * Number1
          + Total_Number1AndNumber2 * Number2 * Number1) / 2;
      int total = sumTotal_Number1 + sumTotal_Number2 - sumTotal_Number1andNumber2;
      Console.WriteLine("Sum of all the multiples of 3 or 5 below or equal to 1000 " +
                      "using arithmetic progression’s sum formula  " + total);

      Console.ReadLine();
    }
  }
}

Please post your comments if you find a better way to find the sum of numbers which are divisible by 3 or 5 and less than 1000.

Disable Internet Explorer Friendly Error Messages

Sometimes Internet Explorer display friendly error messages like “Internet explorer cannot display this webpage”. Via looking into this error message we do not get, any idea related to error message and most people think, it’s an internet connection problem.

In order to view the actual error message, we need to disable “Internet Explorer’s friendly error message” feature. In order to disable this feature go to Tools menu, click Internet Options, click the resulting dialog’s Advanced tab, and clear the “Show friendly HTTP error messages” checkbox.