简体   繁体   English

带C#素数的ASP

[英]Asp with C# Prime Number

I'm a 17 year old student currently in software engineering and web development and im having trouble right now with some of my coding. 我是一名17岁的学生,目前从事软件工程和网络开发,现在我的一些编码遇到麻烦。 I need to make a project that will alow the user to input a number anywherefrom 0 to 999 and tell whether it is a prime number or not. 我需要制作一个项目,使用户可以输入一个介于0到999之间的数字,并告诉它是否是质数。 The code i have so far is.... 我到目前为止的代码是...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void primeNumber()

    {

        int primeNumber1 = int.Parse(Request.Form["Text4"]);

        if (primeNumber1 % 1 == 0 &  !           (primeNumber1 % 2 == 0 &
                                                  primeNumber1 % 3 == 0 &
                                                  primeNumber1 % 4 == 0 &
                                                  primeNumber1 % 5 == 0 &
                                                  primeNumber1 % 6 == 0 &
                                                  primeNumber1 % 7 == 0 &
                                                  primeNumber1 % 8 == 0 &
                                                  primeNumber1 % 9 == 0))
                {
            Response.Write(" This is a prime number! ");
        }

        else
        {
            Response.Write(" This is not a prime Number! ");
        }


    }
}

... but i cannot get this program to display the correct answer. ...但是我无法让该程序显示正确的答案。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

You have got the concept of prime numbers wrong. 您对质数的概念有误。 Your code would for example report that 3 is not a prime number, because you check if the number divides evenly in three even if the number entered is three. 例如,您的代码会报告3不是质数,因为即使输入的数字为3,您也要检查数字是否被三等分。

The simplest solution would be to loop from 2 and up to primeNumber1 - 1 and check if any of those divides evenly with the number. 最简单的解决方案是从2循环到primeNumber1 - 1然后检查其中的任何一个是否与数字primeNumber1 - 1 As you are using a loop, you also need a variable to hold what the result was, as you don't have a single expression that returns the result. 使用循环时,由于没有单个返回结果的表达式,因此还需要一个变量来保存结果。

Something like: 就像是:

bool prime = true;
for (int i = 2; i <= primeNumber1 - 1; i++) {
  if (primeNumber1 % i == 0) {
    prime = false;
  }
}

This is of course the simplest possible solution that solves the problem, for reasonably small numbers. 当然,对于相当小的数目,这是解决问题的最简单的解决方案。 You can for example improve on the solution by exiting out of the loop as soon as you know that it's not a prime number. 例如,您可以在知道不是质数的情况下退出循环,从而改进解决方案。

You also don't need to loop all the way to primeNumber1 - 1 , but only as high as the square root of the number, but you can find out about that if you read up on methods for checking prime numbers. 您也不需要一直循环到primeNumber1 - 1 ,而只需循环到数字的平方根即可,但是如果您阅读了检查质数的方法,就可以找到答案。

You need to handle the special cases of 1 and 2 also. 您还需要处理1和2的特殊情况。 By definition 1 is not a prime number, but 2 is. 根据定义,1不是质数,而2是质数。

http://en.wikipedia.org/wiki/Prime_number http://en.wikipedia.org/wiki/Prime_number

Anytime you find yourself in programming repeating a test on a sequential range of numbers you're doing the wrong thing. 每当您发现自己在编程中对一系列数字重复测试时,您所做的事情都是错误的。 A better construct for this is a loop. 更好的构造方法是循环。 This will give you the range of numbers in an identifier which can then be used to write the repetive code one time. 这将为您提供标识符中的数字范围,该标识符随后可用于一次编写重复代码。 For example I could rewrite this code 例如我可以重写这段代码

primeNumber1 % 2 == 0 &
primeNumber1 % 3 == 0 &
primeNumber1 % 4 == 0 &
primeNumber1 % 5 == 0 &
primeNumber1 % 6 == 0 &
primeNumber1 % 7 == 0 &
primeNumber1 % 8 == 0 &
primeNumber1 % 9 == 0))

As follows 如下

bool anyFactors = false;
for (int i = 2; i <= 9; i++) {
  if (primeNumber1 % i != 0) { 
    anyFactors = true;
    break; 
  }
}

At this point I can now substitute the value allTrue for the original condition you wrote. 现在,我现在可以将值allTrue为您编写的原始条件。

if (primeNumber1 % 1 == 0 && !anyFactors)

I can also expand the number of values tested here by substiting a different number for the conditional check of the loop. 我还可以通过为循环的条件检查替换一个不同的数字来扩展此处测试的值的数量。 If I wanted to check 999 values I would instead write 如果我想检查999个值,我会写

for (int i = 2; i <= 999; i++) { 
  ...
}

Additionally you don't want to use & in this scenario. 此外&在这种情况下,您不想使用& That is for bit level and operations. 这是针对位级别and操作的。 You are looking for the logical and operator && 您正在寻找逻辑和运算符&&

bool IsPrime(int number) {

    if (number == 1) return false;
    if (number == 2) return true;

    for (int i = 2; i < number; ++i)  {
       if (number % i == 0)  return false;
    }

    return true;

}

A little google-fu or a little navel-gazing about prime numbers in general, will lead you to the naive algorithm: 一般而言,关于素数的一些google-fu或一些肚脐式的注视,将带您进入朴素的算法:

For all n such that 0 < n: 对于所有n使得0 <n:

  • There are two "special case" prime numbers, 1 and 2 . 有两个“特殊情况”素数,即12
  • All even numbers > 2 are non-prime, by definition 根据定义,所有大于2的偶数都不是素数
  • If you think about the nature of factoring, the largest possible factor you have to consider is the square root of n , since above that point, the factors are reflexive (ie, the possible factorizations of 100 are 1*100 , 2*50 , 4*25 , 5*20 , 10*10 , 20*5 , 25*4, 50*2 and 100*1 — and the square root of 100 is...10). 如果考虑分解的性质,则必须考虑的最大可能因子是n平方根,因为在该点之上,因子是自反的(即,可能的100因子分解为1 * 100,2 * 50, 4 * 25,5 * 20,10 * 10,20 * 5,25 * 4、50 * 2和100 * 1-100的平方根是... 10)。

That should lead you to an implementation that looks something like this: 那应该导致您实现一个类似于以下内容的实现:

static bool IsPrime( int n )
{
  if ( n < 1 ) throw new ArgumentOutOfRangeException("n") ;

  bool isPrime = true ;
  if ( n > 2 )
  {

    isPrime = ( 0 != n & 0x00000001 ) ; // eliminate all even numbers

    if ( isPrime )
    {
      int limit = (int) Math.Sqrt(n) ;
      for ( int i = 3 ; i <= limit && isPrime ; i += 2 )
      {
        isPrime = ( 0 != n % i ) ;
      }
    }
  }
  return isPrime ;
}

Try the code below: 请尝试以下代码:

bool isPrimeNubmer(int n)
{
    if (n >=0 && n < 4) //1, 2, 3 are prime numbers
        return true;
    else if (n % 2 == 0) //even numbers are not prime numbers
        return false;
    else
    {
        int j = 3;
        int k = (n + 1) / 2 ;

        while (j <= k)
        {
            if (n % j == 0)
                return false;
            j = j + 2;
        }
        return true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM