计算一百万个素数(to calculate one million prime numbers)
我有一个问题要打印一百万个素数。 我已经写了一个Java程序..它目前需要约1.5分钟来计算它..我认为我的解决方案不是那么高效。 我使用了下面的算法:
- 首先将1 2 3添加到主要列表中
- 计算要检查的号码的最后一位数字
- 检查数字是0,2或4还是6或8,然后跳过数字
- 否则计算数字的平方根。
- 尝试将数字从2开始划分,直到数字的平方根
- 如果数字是可整除的,则跳过数字,然后将其添加到素数列表中
我也读过其他几种解决方案,但我没有找到一个好的答案。 请理想地建议应该是什么应该是大约最短的时间来计算这个和需要做出什么样的改变,使算法更有效率。
I have got one question to print one million prime numbers . I have written a java program for that .. It's currently taking 1.5 mins approx to calculate it .. I think my solution is not that efficient. I have used the below algo:
- Adding 1 2 3 to the prime list initially
- Calculating the last digit of the number to be checked
- Checking if the digit is 0 , 2 or 4 or 6 or 8 then skipping the number
- else calculating the square root of the number ..
- Trying to Divide the number starting from 2 till the square root of the number
- if number is divisible then skipping the number else adding it to the prime list
I have read several other solutions as well , but I didn't find a good answer. Please suggest ideally what should be approx minimum time to calculate this and what changes are required to make the algorithm more efficient.
原文:https://stackoverflow.com/questions/13404389
最满意答案
这是因为特异性。 你可以使用
!important
作为其他帖子的建议,但在CSS中使用!important
是一种不好的做法。Use that as you last option.
而是使用两个类..
确保最内层的li具有
default
类。$('#menu ul li ul li').addClass('default').hover( function() { $(this).addClass('whitebg').removeClass('default'); }, function () { $(this).removeClass('whitebg').addClass('default'); } );
This is because of the specificity. you can use
!important
as the other posts suggested but using!important
in your CSS is a bad practice.Use that as you last option.
Instead use two classes..
Make sure the inner most li has the
default
class to it..$('#menu ul li ul li').addClass('default').hover( function() { $(this).addClass('whitebg').removeClass('default'); }, function () { $(this).removeClass('whitebg').addClass('default'); } );
相关问答
更多-
Jquery的attr()和css()和addclass()的区别[2021-06-07]
亲,很高兴为你回答 addclass() 方法向被选元素添加一个或多个类。 该方法不会移除已存在的 class 属性,仅仅添加一个或多个 class 属性。 attr() 方法设置或返回被选元素的属性值。 根据该方法不同的参数,其工作方式也有所差异。 css() 为所有匹配元素的给定 CSS 属性设置值 亲,有问题继续追问我哦。。。 -
jQuery / css addClass()风格覆盖以前的样式属性(jQuery / css addClass() style overriding previous style property)[2023-05-22]
问题是ID会覆盖类定义。 更改类中的背景而不是id the problem is that ids override class definitions. change the background in classes instead of ids -
你可以这样做: $('#yourElement').addClass(function(index, currentClass) { return $(this).css('position') == 'absolute' ? 'xxx' : ''; }); 有关语法的更多信息,请查看.addClass()的API 。 You could do it this way: $('#yourElement').addClass(function(index, currentClass) { r ...
-
你可以添加width : 0%; .Sliders风格并添加到.active类的转换 $(document).ready(function() { $('.Sliders').click(function(e) { $(this).addClass('active').siblings().removeClass('active'); }); }); #SliderHolder { width: 100%; height: 100%; font-size: 0px; ...
-
您的selector不正确: $(document).ready (function() { $("theDiv").addClass("rotate"); }); 应该: $(document).ready (function() { $("#theDiv").addClass("rotate"); }); 为了获得最大的兼容性,你应该添加-ms-transform: rotate(180deg)和-webkit-transform: rotate(180deg); 。 有了这些改变,它对我有 ...
-
必须将active类分配给li元素,在这种情况下,它是share_links_tab和create_links_tab元素的父元素 $(document).ready(function () { $('#create_links_tab').click(function () { $('#share_links_tab').parent().removeClass("active"); $('#create_links_tab').parent().addClass ...
-
这是因为特异性。 你可以使用!important作为其他帖子的建议,但在CSS中使用!important是一种不好的做法。 Use that as you last option. 而是使用两个类.. 确保最内层的li具有default类。 $('#menu ul li ul li').addClass('default').hover( function() { $(this).addClass('whitebg').removeClass('default'); }, f ...
-
.mainNavSelected { float:left; margin-right:5px; color:white; font-family:Tahoma; font-weight:bold; padding: 10px 8px 0px 8px; background: #660000 !important; height:40px; height: 25px !important; border:1px ...
-
只需使用嵌入式CSS,即我们页面上的css中的#header 。 如果您尝试使用JavaScript应用CSS,则如果禁用浏览器上的JavaScript,则您的页面可能无法正确显示。 您可以使用外部CSS文件,头部嵌入式CSS和内联CSS。 始终优先考虑纯HTML和CSS。 相信我,它更快。 Just use Embedded CSS, that is #header in css on our page. If you try to apply CSS using JavaScript, your pag ...
-
这是因为您之前的选择器具有更高的特异性。 您可以将.dark-tab CSS选择器更改为#tabs li.dark-tab以匹配先前规则的特异性,或者您可以使用!important来覆盖先前规则的特殊性,如 .dark-tab { background-color: #C2C3C4 !important; } This is because your previous selector has higher specificity. You can change your CSS selector f ...