突出显示素数 [英] Highlight the prime numbers

查看:73
本文介绍了突出显示素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#或Asp.Net突出显示给定数字的素数





 1 2 
25 203
35 145
56 22
78 45
9 -1





我尝试了什么:



这是我到目前为止所做的:但不幸的是它是有史以来最糟糕的代码。

  var  prime = function(){
var num;
for (num = 0 ; num < ; 101 ; num ++){
if (num% 2 === 0 ){
break ;
}
else if (num% 3 === 0 ){
break ;
}
else if (num% 4 === 0 ){
break ;
}
else if (num% 5 === 0 ){
break ;
}
else if (num% 6 === 0 ){
break ;
}
else if (num% 7 === 0 ){
break ;
}
else if (num% 8 === 0 ){
break ;
}
else if (num% 9 === 0 ){
break ;
}
else if (num% 10 === 0 ){
break ;
}
else if (num% 11 === 0 ){
break ;
}
else if (num% 12 === 0 ){
break ;
}
else {
return num;
}
}
};
console.log(prime());

解决方案

引用:

这是我到目前为止所做的:但遗憾的是它是有史以来最糟糕的代码。

我同意你的意见,这是有史以来最糟糕的代码,甚至没有工作甚至不确定它是C#,它看起来像JS。



你的代码可以通过删除非素数除数的测试来简化,它会给出相同的结果

  var  prime =  function (){
var num;
for (num = 0 ; num< 101 ; num ++){
if (num% 2 === 0 ){
break ;
}
else if (num% 3 === 0 ){
break ;
}
else if (num% 5 === 0 ){
break ;
}
else if (num% 7 === 0 ){
break ;
}
else if (num% 11 === 0 ){
break ;
}
else {
return num;
}
}
};
console .log(prime());



如我所见,这段代码没有甚至发表声明。



我们不做你的家庭作业。

HomeWork不会在乞求别人做的时候测试你的技能你的工作,它会让你思考并帮助你的老师检查你对你所学课程的理解,以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,试一试,重读你的课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。越快越好。

创建一个算法基本上是找到数学并进行必要的调整以适应你的实际问题。



做一些研究也是你工作的一部分。

素数 - 维基百科 [ ^ ]

整数分解 - 维基百科 [ ^ ]


Highlight the prime numbers in given numbers using C#or Asp.Net


1	2
25	203
35	145
56	22
78	45
9	-1



What I have tried:

this is what i have so far: but unfortunately it is the worst code ever.

var prime = function (){
var num;
for (num = 0; num < 101; num++){
    if (num % 2 === 0){
        break;
    }
    else if (num % 3 === 0){
        break;
    }
    else if (num % 4=== 0){
        break;
    }
    else if (num % 5 === 0){
        break;
    }
    else if (num % 6 === 0){
        break;
    }
    else if (num % 7 === 0){
        break;
    }
    else if (num % 8 === 0){
        break;
    }
    else if (num % 9 === 0){
        break;
    }
    else if (num % 10 === 0){
        break;
    }
    else if (num % 11 === 0){
        break;
    }
    else if (num % 12 === 0){
        break;
    }
    else {
        return num;
    }
}
};
console.log(prime());

解决方案

Quote:

this is what i have so far: but unfortunately it is the worst code ever.

I agree with you, the worst code ever and it don't even work and not even sure it is C#, it look like JS.

Your code can be simplified by removing test for non prime divisors, it will give same results

var prime = function (){
var num;
for (num = 0; num < 101; num++){
    if (num % 2 === 0){
        break;
    }
    else if (num % 3 === 0){
        break;
    }
    else if (num % 5 === 0){
        break;
    }
    else if (num % 7 === 0){
        break;
    }
    else if (num % 11 === 0){
        break;
    }
    else {
        return num;
    }
}
};
console.log(prime());


As I see, this code do not even mach the statement.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

Doing some research is also part of your job.
Prime number - Wikipedia[^]
Integer factorization - Wikipedia[^]


这篇关于突出显示素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆