C#功能接受整数n和返回最低数量,可划分的数字1到n

0

的问题

我需要编写一个函数,需要作为一个参数的数目n和返回(string)的最低数量,可划分所有数从1到n. 例如,如果n=4的功能将返回12 12月4日12/3 12/2 12/1是整数。

我已经写了一个功能,其正常工作时数少于19..上面的19所述的计算时间变得更长的时间。 有人能给我一个暗示如何更好的机制,这一功能做的更快

 public static string Smallest(int n)
        {
           
            int good = 0;//will hold number of times we got divide with no remianders
            int num = n;//smallest possible number is n
            while (true)
            {
                good = 0;
                for (int i=n; i>=1; i--)
                {
                    if (num % i ==0) good++;//meaning we got zero remainder for the divide
                    if (good == n) return num.ToString();//num of times we got zero remainders == n.

                }
                num++;
            }

        }


algorithm c# math
2021-11-23 18:05:46
3

最好的答案

1

你要有 巨大的 数字大 n这就是为什么我们使用 BigInteger 内部计算。 作为阿布舍克*潘迪把它,我们应该计算 LCM,这可以获得

 LCM(a, b) = a * b / GCD(a, b)

在CGD是最大公约数

代码:

using System.Numerics;

...

public static string Smallest(int n) {
  if (n < 1)
    throw new ArgumentOutOfRangeException(nameofn()); 

  BigInteger result = 1;

  for (int i = 1; i <= n; ++i) 
    result = result * i / BigInteger.GreatestCommonDivisor(result, i);

  return result.ToString();
}

演示:

  using System.Linq;
  using System.Numerics;

  ...

  var demos = string.Join(Environment.NewLine, Enumerable
    .Range(1, 20)
    .Select(n => $"{n,2} : {Smallest(n),20}"));

  Console.WriteLine(demos);
  Console.WriteLine();
  Console.WriteLine(Smallest(100));

结果:

 1 :                    1
 2 :                    2
 3 :                    6
 4 :                   12
 5 :                   60
 6 :                   60
 7 :                  420
 8 :                  840
 9 :                 2520
10 :                 2520
11 :                27720
12 :                27720
13 :               360360
14 :               360360
15 :               360360
16 :               720720
17 :             12252240
18 :             12252240
19 :            232792560
20 :            232792560

69720375229712477164533808935312303556800
2021-11-23 18:37:03
1

我的逻辑:

  1. 我们拿一个号码-这是最少数量有什么可以返回
  2. 数-1-如果它不能划分没有提醒加n初始n

不要忘记更新数到初始时2步骤已经提醒

这样做,直到你得到正确的价值

2021-11-23 18:29:42
1

你需要找到液晶模(最低的共同多个)的所有号码 1 to n.

这里是一个很好的例子找到液晶模的一系列要素。 https://www.geeksforgeeks.org/lcm-of-given-array-elements/

你可以创建一个列有所有的数从1到n并通过它向这种功能。

你可以修改它仅通过 n 并使它有效使用情况。

2021-11-23 18:22:57

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................