获取连续因子,c#

发布于 2024-07-27 14:04:42 字数 1863 浏览 1 评论 0原文

我需要解决这个问题,但我一直在获取因子,但我需要做的是...

正数 n 是连续因子的当且仅当它有因子 i 和 j ,其中 i > 。 1、j> 1 且 j = i + 1。编写一个名为 isConsecutiveFactored 的函数,如果其参数是连续因子分解的,则返回 1,否则返回 0。 函数签名是 int isConsectiveFactored(int n)

函数签名是 int isConceptiveFactored(int n) 示例

如果 n 为 24,则返回 1,因为 24 = 2*3*4 且 3 = 2 + 1

如果 n 为 105,则返回 0,因为 105 = 3*5 *7 和 5 != 3+1 和 7 != 5+1

如果 n 是 90,则返回 1,因为 90 的因数包括 2 和 3,并且 3 = 2 + 1

到目前为止能够得到因子,即如果数字是 24 那么我能够得到 2 和 12,但我卡在那里,一片空白......

 using System;
  using System.Collections.Generic;
  using System.Text;

 namespace isConsecutiveFactored
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(isConsecutiveFactored(24));
    }

    private static int isConsecutiveFactored(int p)
    {


        foreach (int a1 in getFactor(24))
        {
            Console.WriteLine(a1);
        }



        return 0;

    }

    private static List<int> getFactor(int p)
    {
        List<int> factor = new List<int>();
        int max = (int)Math.Sqrt(p);
        for (int i = 1; i <= max; i++)
        {
            if (i != 0)
            {
                if ((p % i) == 0)
                {

                    if (i != max)
                    {
                        if ((p / i) != 1 && (p / i) != p)
                        {
                            factor.Add(i);
                            factor.Add(p / i);
                            //Console.WriteLine((p / i) + "  " + "this is the factor");
                        }
                    }

                }
            }

            //
        }
        List<int> fac = factor.GetRange(0, 2);


        return fac;
    }
}

}

任何人都可以帮我解决这个问题......

i need to solve this question but im stuck at getting the factors , but what i need to do is...

A positive number n is consecutive-factored if and only if it has factors, i and j where i > 1, j > 1 and j = i + 1. Write a function named isConsecutiveFactored that returns 1 if its argument is consecutive-factored, otherwise it returns 0.
the function signature is
int isConsectiveFactored(int n)

the function signature is
int isConsectiveFactored(int n)
Example

If n is 24 return 1 because 24 = 2*3*4 and 3 = 2 + 1

If n is 105 return 0 because 105 = 3*5*7 and 5 != 3+1 and 7 != 5+1

If n is 90 return 1 because factors of 90 include 2 and 3 and 3 = 2 + 1

so far ive been able to get the factor i.e if the number is 24 then ive been able to get
2 and 12 , but im stuck there and blanked....

 using System;
  using System.Collections.Generic;
  using System.Text;

 namespace isConsecutiveFactored
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(isConsecutiveFactored(24));
    }

    private static int isConsecutiveFactored(int p)
    {


        foreach (int a1 in getFactor(24))
        {
            Console.WriteLine(a1);
        }



        return 0;

    }

    private static List<int> getFactor(int p)
    {
        List<int> factor = new List<int>();
        int max = (int)Math.Sqrt(p);
        for (int i = 1; i <= max; i++)
        {
            if (i != 0)
            {
                if ((p % i) == 0)
                {

                    if (i != max)
                    {
                        if ((p / i) != 1 && (p / i) != p)
                        {
                            factor.Add(i);
                            factor.Add(p / i);
                            //Console.WriteLine((p / i) + "  " + "this is the factor");
                        }
                    }

                }
            }

            //
        }
        List<int> fac = factor.GetRange(0, 2);


        return fac;
    }
}

}

can anybody help me with this .....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说好的呢 2024-08-03 14:04:42

尝试以下操作

public static bool IsConsequtiveFactor(int number) {
  var factors = GetFactors(number);
  int? last = null;
  foreach ( var cur in factors ) {
    if ( last.HasValue && last.Value == cur - 1 ) {
      return true;
    }
    last = cur;
  }  
}

public static IEnumerable<int> GetFactors(int number) {
  int max = (int)Math.Sqrt(number);
  return Enumerable
    .Range(2,max-2)
    .Where(x => 0 == number % x);
}

Try the following

public static bool IsConsequtiveFactor(int number) {
  var factors = GetFactors(number);
  int? last = null;
  foreach ( var cur in factors ) {
    if ( last.HasValue && last.Value == cur - 1 ) {
      return true;
    }
    last = cur;
  }  
}

public static IEnumerable<int> GetFactors(int number) {
  int max = (int)Math.Sqrt(number);
  return Enumerable
    .Range(2,max-2)
    .Where(x => 0 == number % x);
}
放赐 2024-08-03 14:04:42
    public static bool IsConsecutiveFactored(int number)
    {
        var ints = Factor(number);
        return (from i in ints join s in ints on i equals s + 1 
                where i > 1 && s > 1
                select i).Count() > 0;
    }

    public static IEnumerable<int> Factor(int number)
    {
        int max = (int)Math.Sqrt(number);  //round down
        for (int factor = 1; factor <= max; ++factor)
        { //test from 1 to the square root, or the int below it, inclusive.
            if (number % factor == 0)
            {
                yield return factor;
                if (factor != max)
                { // Don't add the square root twice!  Thanks Jon
                    yield return number / factor;
                }
            }
        }
    }

但你真的应该自己做作业,而我无法让自己返回一个 int。

    public static bool IsConsecutiveFactored(int number)
    {
        var ints = Factor(number);
        return (from i in ints join s in ints on i equals s + 1 
                where i > 1 && s > 1
                select i).Count() > 0;
    }

    public static IEnumerable<int> Factor(int number)
    {
        int max = (int)Math.Sqrt(number);  //round down
        for (int factor = 1; factor <= max; ++factor)
        { //test from 1 to the square root, or the int below it, inclusive.
            if (number % factor == 0)
            {
                yield return factor;
                if (factor != max)
                { // Don't add the square root twice!  Thanks Jon
                    yield return number / factor;
                }
            }
        }
    }

But you should really do your homework yourself, and I couldn't bring myself to return an int.

〃温暖了心ぐ 2024-08-03 14:04:42

尝试这个

static int IsConsectiveFactored(int n) {
    for (int i = 2; i < n; i++) {
        if (n % i == 0 && n % (i + 1) == 0)
            return 1;
    }
    return 0;
}

Try this

static int IsConsectiveFactored(int n) {
    for (int i = 2; i < n; i++) {
        if (n % i == 0 && n % (i + 1) == 0)
            return 1;
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文