How to resolve the algorithm Proper divisors step by step in the C# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Proper divisors step by step in the C# programming language

Table of Contents

Problem Statement

The   proper divisors   of a positive integer N are those numbers, other than N itself, that divide N without remainder. For N > 1 they will always include 1,   but for N == 1 there are no proper divisors.

The proper divisors of     6     are   1, 2, and 3. The proper divisors of   100   are   1, 2, 4, 5, 10, 20, 25, and 50.

Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Proper divisors step by step in the C# programming language

The given C# code is a program that determines the proper divisors of numbers within a given range (1 to 10) and identifies the number with the highest number of proper divisors within a larger range (1 to 20000).

  • ProperDivisors Method:

    • Calculates the proper divisors of a given number. Proper divisors are positive divisors excluding the number itself.
    • It uses the Enumerable.Range and Enumerable.Where methods to generate a sequence of potential divisors and filter out those that divide evenly into the given number.
  • Main Method:

    • Iterates through numbers from 1 to 10 and prints their proper divisors.
    • Finds the number between 1 and 20000 with the highest number of proper divisors using LINQ. It creates anonymous objects to store the number and the count of its proper divisors, sorts the list in descending order based on the count, and selects the first item.
  • Execution:

    • The program outputs the proper divisors for each number from 1 to 10, separated by commas.
    • Finally, it displays the number with the highest number of proper divisors within the range 1 to 20000.

Source code in the csharp programming language

namespace RosettaCode.ProperDivisors
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal static class Program
    {
        private static IEnumerable<int> ProperDivisors(int number)
        {
            return
                Enumerable.Range(1, number / 2)
                    .Where(divisor => number % divisor == 0);
        }

        private static void Main()
        {
            foreach (var number in Enumerable.Range(1, 10))
            {
                Console.WriteLine("{0}: {{{1}}}", number,
                    string.Join(", ", ProperDivisors(number)));
            }

            var record = Enumerable.Range(1, 20000).Select(number => new
            {
                Number = number,
                Count = ProperDivisors(number).Count()
            }).OrderByDescending(currentRecord => currentRecord.Count).First();
            Console.WriteLine("{0}: {1}", record.Number, record.Count);
        }
    }
}


  

You may also check:How to resolve the algorithm Pell numbers step by step in the Haskell programming language
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the Gema programming language
You may also check:How to resolve the algorithm Animation step by step in the Racket programming language
You may also check:How to resolve the algorithm Animation step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Scheme programming language