gpt4 book ai didi

get the consecutive factors ,c#(得到连续因子,c#)

转载 作者:bug小助手 更新时间:2023-10-25 12:51:43 24 4
gpt4 key购买 nike



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)

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



the function signature is
int isConsectiveFactored(int n)
Example

函数签名为int isConsectiveFactored(Int N)示例



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

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



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

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



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

如果n为90,则返回1,因为90的因数包括2和3,且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....

到目前为止,我能够得到这个因子,即如果数字是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;
}
}


}

}



can anybody help me with this .....

有谁能帮我这个忙吗……


更多回答
优秀答案推荐

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);
}


    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.

但你真的应该自己做功课,我不能让自己返回一个整型。



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;
}

更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com