C++研究
C++細(xì)節(jié)深度探索及軟件工程
C++博客
::
首頁
::
新隨筆
::
聯(lián)系
::
聚合
::
管理
::
37 隨筆 :: 0 文章 :: 74 評(píng)論 :: 0 Trackbacks
<
2007年6月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
公告
致力于百度無線搜索研發(fā)。
常用鏈接
我的隨筆
我的評(píng)論
我參與的隨筆
留言簿
(8)
給我留言
查看公開留言
查看私人留言
隨筆分類
(19)
ACM(1)
(rss)
Algorithm(4)
(rss)
Design Patterns & Engeering(4)
(rss)
STL(4)
(rss)
小技巧(6)
(rss)
隨筆檔案
(37)
2010年3月 (1)
2009年8月 (1)
2009年3月 (1)
2008年10月 (1)
2008年3月 (1)
2008年2月 (2)
2008年1月 (3)
2007年12月 (2)
2007年9月 (4)
2007年7月 (2)
2007年6月 (6)
2007年5月 (2)
2007年4月 (11)
相冊
深愛著的母校-天津大學(xué)
收藏夾
Goodies
(rss)
好友連接
丑石
(rss)
趙博的Blog連接
學(xué)術(shù)算法研究
最新隨筆
1.?[征集]如果百度無線搜索計(jì)劃產(chǎn)出新產(chǎn)品,你最希望是什么?
2.?百度阿拉丁指亮暗網(wǎng),何為暗網(wǎng)?
3.?關(guān)于http://wap.baidu.com招喚
4.?轉(zhuǎn)入Apache2開發(fā),淡忘windows了
5.?VC的Dialog或FormView中的控件不能刷新
6.?關(guān)閉Linux下終端或Vi的BEEP聲
7.?unbuntu Linux下切換GDM與KDM
8.?求有序序列公共部分(集合交集的O(n)復(fù)雜度求法)
9.?對包含Struct的Vector就其中的一種屬性排序
10.?B樹ReadKey關(guān)鍵點(diǎn)的操作與實(shí)現(xiàn)
搜索
積分與排名
積分 - 70322
排名 - 330
最新評(píng)論
1.?re: 求有序序列公共部分(集合交集的O(n)復(fù)雜度求法)
取交集可不是這么取的吧?一趟循環(huán)即可!
--黃智
2.?re: Mysql++使用手冊及用法規(guī)范[未登錄]
好東西
--樂樂
3.?re: Mysql++使用手冊及用法規(guī)范
好東西,值得鼓勵(lì)
--njf
4.?re: Mysql++使用手冊及用法規(guī)范
必然頂樓主!!!!!!!
--擴(kuò)大客戶繳費(fèi)
5.?re: Mysql++使用手冊及用法規(guī)范
謝了~ 瞅瞅先!
--kongkong
閱讀排行榜
1.?有關(guān) C++ 嵌套類 (7811)
2.?關(guān)于http://wap.baidu.com招喚(7006)
3.? C++ streams (How to use ostream & istream ?)(6898)
4.?Mysql++使用手冊及用法規(guī)范(6590)
5.?VC的Dialog或FormView中的控件不能刷新(3123)
6.?[資料]STL種容器的基本使用方法(2723)
7.?string 類的使用方法(2574)
8.?精煉循環(huán)右移(2507)
9.?求有序序列公共部分(集合交集的O(n)復(fù)雜度求法)(2176)
10.?對包含Struct的Vector就其中的一種屬性排序(2157)
評(píng)論排行榜
1.?Mysql++使用手冊及用法規(guī)范(25)
2.?Some algorithms about judging a prime .(8)
3.?精煉循環(huán)右移(6)
4.?Implement "GOF's Builder pattern" Using C++(Series of Gof patterns using C++ 4th article) (4)
5.?Implement "GOF's Adapter pattern" Using C++(Series of Gof patterns using C++ 2nd article) (4)
6.?[征集]如果百度無線搜索計(jì)劃產(chǎn)出新產(chǎn)品,你最希望是什么?(4)
7.?關(guān)于http://wap.baidu.com招喚(3)
8.?有關(guān) C++ 嵌套類 (3)
9.? C++ streams (How to use ostream & istream ?)(3)
10.?Math For Programmers (2)
How can you efficeny judge whether the num is primer?
A friend ask me 'How can you efficeny judge whether the num is primer? '
There is a easy way to do it , the follow code isn't written by me but a classic method
E.G quote from
STL tutorial reference
#include
<
iostream
>
#include
<
list
>
#include
<
algorithm
>
#include
<
cstdlib
>
//
for abs()
using
namespace
std;
//
predicate, which returns whether an integer is a prime number
bool
isPrime (
int
number)
{
//
ignore negative sign
number
=
abs(number);
//
0 and 1 are prime numbers
if
(number
==
0
||
number
==
1
)
{
return
true
;
}
//
find divisor that divides without a remainder
int
divisor;
for
(divisor
=
number
/
2
; number
%
divisor
!=
0
;
--
divisor)
{
;
}
//
if no divisor greater than 1 is found, it is a prime number
return
divisor
==
1
;
}
posted on 2007-04-19 02:39
常興龍
閱讀(333)
評(píng)論(1)
編輯
收藏
引用
所屬分類:
Algorithm
評(píng)論
#
re: How can you efficeny judge whether the num is primer?
2008-07-05 16:31
我們一起來提高
我的看法:
(1)0和1都不算素?cái)?shù)。
(2)2和3都是素?cái)?shù),可以直接返回。
(3)判斷一個(gè)比較小的數(shù)(可以認(rèn)為在long范圍內(nèi)的吧,如果是1024二進(jìn)制位的大數(shù)就得想別的辦法了)是不是素?cái)?shù)肯定得窮舉,提高效率就得根據(jù)已知的條件縮小窮舉的空間。我認(rèn)為以上程序的窮舉空間還不夠小。
其實(shí)只要對大于3的數(shù)num窮舉從2~sqrt(num)就夠了,而不用擴(kuò)展到num/2。假如判斷1122是不是素?cái)?shù),只需要窮舉1122對2~33這個(gè)范圍的余數(shù)有沒有為0的就可以,超過33的數(shù),如果正好有另一個(gè)積數(shù),那么這個(gè)積數(shù)一定小于sqrt(num),乘法運(yùn)算是對稱的,這是數(shù)實(shí)際上已經(jīng)窮舉過了。這樣以1122為例就少窮舉了528次,效率提高了約17倍,而這個(gè)num越大,效率提高就越明顯。
不知道我說的對不對,請大家指教啊。
回復(fù)
更多評(píng)論
刷新評(píng)論列表
只有注冊用戶
登錄
后才能發(fā)表評(píng)論。
【推薦】100%開源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
求有序序列公共部分(集合交集的O(n)復(fù)雜度求法)
精煉循環(huán)右移
Some algorithms about judging a prime .
How can you efficeny judge whether the num is primer?
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Powered by:
C++博客
Copyright © 常興龍
>
hi的博客
国产亚洲美女精品久久久
|
亚洲а∨天堂久久精品9966
|
久久强奷乱码老熟女网站
|
一本一道久久a久久精品综合
|
亚洲国产成人久久综合野外
|
亚洲欧美国产精品专区久久
|
久久综合88熟人妻
|
久久精品国产99国产精品澳门
|
国产精品永久久久久久久久久
|
久久精品卫校国产小美女
|
久久99中文字幕久久
|
日日狠狠久久偷偷色综合免费
|
久久精品中文字幕无码绿巨人
|
青青青国产精品国产精品久久久久
|
国产精品伦理久久久久久
|
狠狠色伊人久久精品综合网
|
亚洲中文字幕久久精品无码喷水
|
久久亚洲精品视频
|
亚洲精品99久久久久中文字幕
|
精品久久久久久久无码
|
久久永久免费人妻精品下载
|
日韩欧美亚洲国产精品字幕久久久
|
99久久人妻无码精品系列蜜桃
|
久久免费视频1
|
亚洲熟妇无码另类久久久
|
久久久无码精品午夜
|
国产精品久久毛片完整版
|
少妇熟女久久综合网色欲
|
久久国产香蕉视频
|
国产精品九九久久精品女同亚洲欧美日韩综合区
|
亚洲国产成人久久一区WWW
|
2020最新久久久视精品爱
|
久久精品国产亚洲AV麻豆网站
|
亚洲乱码日产精品a级毛片久久
|
伊人久久大香线蕉AV一区二区
|
久久青青草原综合伊人
|
久久青青草原国产精品免费
|
久久这里只有精品首页
|
精品久久久久久无码不卡
|
韩国三级中文字幕hd久久精品
|
国产福利电影一区二区三区久久久久成人精品综合
|