C#练习题答案: 威尔逊质数【难度:0级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

威尔逊质数【难度:0级】:

答案1:

using System;
public class Kata {
  public static bool AmIWilson(int p) {
    return Array.Exists(new int[]{5, 13, 563}, e => e == p);
  }
}

答案2:

public class Kata
{
  public static bool AmIWilson(int p)
  {
    int pow = 1;
    for (int i = 2; i < p; i++)
      pow = pow * i;
    if ((pow + 1)%(p * p) == 0)
      return true;
    else   
      return false;
  }
}

答案3:

public class Kata
{
  // Only 3 Wilson primes are known, others must be > 2 * 10^13 which is greater than int.MaxValue.
  public static bool AmIWilson(int p) => p == 5 || p == 13 || p == 563;
}

答案4:

public class Kata
{
  public static bool AmIWilson(int p)
  {
    return p == 5 || p == 13 || p == 563;
  }
}

答案5:

public class Kata
{
  public static bool AmIWilson(int p)
  {
     return (p == 5 || p == 13 || p == 563);
  }
}

答案6:

public class Kata
{
  public static bool AmIWilson(int p)
  {
    return (Factorial(p - 1) + 1.0) / (p * p) % 1 == 0 ? true : false;
  }
  
  private static int Factorial(int n)
  {
    if(n > 1) return Factorial(n - 1) * n;
    return 1;
  }
}

答案7:

using System.Linq;
public class Kata
{
  public static bool AmIWilson(int p)
  {
    return ((Enumerable.Range(1, p-1).Aggregate((i1, i2) => i1*i2)+1)%(p*p)) == 0 ? true : false;
  }
}

答案8:

public class Kata
{
  public static bool AmIWilson(int p)
  {
    int F = 1;
    for (int i = 1; i < p; i++)
    {
      F *= i;
    }
    return ((F+1) % (p*p) == 0);
  }
}

答案9:

public class Kata
{
    public static bool AmIWilson(int p)
    {
        int f = 1;
        for (int i = 1; i < p; f *= i++);
        return (f + 1) % (p * p) == 0;
    }
}

答案10:

using System;
using System.Linq;

public class Kata
{
  public static bool AmIWilson(int p) => (1 + Enumerable.Range(1, p - 1).Aggregate(1l, (a,i) => a * i)) % (p * p) == 0;
}

答案11:

//for type int only three exist... (http://mathworld.wolfram.com/WilsonPrime.html)
public class Kata { public static bool AmIWilson(int p) { return p==5||p==13||p==563; } }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值