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 9211681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:21:10+00:00 2026-06-18T01:21:10+00:00

I have a code here written in C# that finds the smallest multiple by

  • 0

I have a code here written in C# that finds the smallest multiple by all numbers from 1 to 20. However, I find it very inefficient since the execution took awhile before producing the correct answer. I would like to know what are the different ways that I can do to improve the code. Thank You.

        public static void SmallestMultiple()
    {
        const ushort ARRAY_SIZE = 21;
        ushort[] array = new ushort[ARRAY_SIZE];
        ushort check = 0;
        for (uint value = 1; value < uint.MaxValue; value++)
        {
            for (ushort j = 1; j < ARRAY_SIZE; j++)
            {
                array[j] = j;
                if (value % array[j] == 0)
                {   
                    check++;
                }
            }
            if (check == 20)
            {
                Console.WriteLine("The value is {0}", value);
            }
            else
            {
                check = 0;
            }
        }
    }
  • 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-18T01:21:11+00:00Added an answer on June 18, 2026 at 1:21 am
        static void Main(string[] args)
        {
            int[] nums = Enumerable.Range(1, 20).ToArray();
            int lcm = 1;
    
            for (int i = 0; i < nums.Length; i++)
            {
                lcm = LCM(lcm, nums[i]);
            }
            Console.WriteLine("LCM = {0}", lcm);
        }
    
        public static int LCM(int value1, int value2)
        {
            int a = Math.Abs(value1);
            int b = Math.Abs(value2);
    
            // perform division first to avoid potential overflow
            a = checked((a / GCD(a, b)));
            return checked((a * b));
        }
    
        public static int GCD(int value1, int value2)
        {
            int gcd = 1;     // Greates Common Divisor
    
            // throw exception if any value=0
            if (value1 == 0 || value2 == 0)
            {
                throw new ArgumentOutOfRangeException();
            }
    
            // assign absolute values to local vars
            int a = Math.Abs(value1);            // local var1
            int b = Math.Abs(value2);            // local var2
    
            // if numbers are equal return the first
            if (a == b) { return a; }
                // if var "b" is GCD return "b"
            if (a > b && a % b == 0) { return b; }
                // if var "a" is GCD return "a"
            if (b > a && b % a == 0) { return a; }
    
            // Euclid algorithm to find GCD (a,b):
            // estimated maximum iterations: 
            // 5* (number of dec digits in smallest number)
            while (b != 0)
            {
                gcd = b;
                b = a % b;
                a = gcd;
            }
            return gcd;
        }
    }
    

    Source : Fast Integer Algorithms: Greatest Common Divisor and Least Common Multiple, .NET solution

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

Sidebar

Related Questions

I have data in multiple columns from rows 3-15. I want to find all
Here is the code I have written for Rectangle class. class Rectangle (l: Double,
I have some code here that uses bitsets to store many 1 bit values
I have this code here printf '$request1 = select * from whatever where this
I have this code here, {foreach from=$cart.cartItems item=item name=cart} <div id=cart2Produkt> <p>{if $item.Product.ID} <a
I have my code here, it works fine from my home, where my user
I have code that uses the BeautifulSoup library for parsing, but it is very
I have written R code that merges two data frames based on first column
I have written a program that is to find a box in another program
I have an SP that work very well when called from SSMS. but when

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.