Regular expressions are extremely powerful. This is something I read at least once or twice every day while reading articles and blogs on the Web. While browsing today, I found this page which thoroughly describes the use of the regular expression /^1?$|^(11+?)\1+$/ in Perl to check if a number is prime or not!!! To be frank, I was skeptical. The regular expression looks like magic! And I wanted to understand it better. I rewrote it in Ruby using irb and I tested it: Great! It also works in Ruby! This means that there is no (Perl) magic going on. The regular expression really works. But how? Let’s try to follow the logic behind it. Is 7 prime? To know this, the function first generates “1111111″ (from “1″ * 7) and tries to see if that string does not match/^1?$|^(11+?)\1+$/. If there is no match, then the number is prime. Notice that the regular expression has two parts (separated with the vertical bar |). The first part is /^1?$/ is trivial and matches with beginning of line (^), an optional 1 (1?) The second part is where the magic occurs… /^(11+?)\1+$/ matches with beginning of line (^) then by (11+?) then by \1+ and finally by end of line ($). I guess that you know that \1 is a variable which is bound to whatever was matched previously (in our case by (11+?)). Let’s proceed slowly… (11+?) does two things \1+ then matches with whatever has been matched above (”11″ initially) repeated one or more times. If this match succeeds then the number is not prime. If you are following, you’ll realise that this eliminates all even numbers except 2 (for example, 8 is “11111111″ and therefore (11+?) will match with “11″ and \1+ will match with “111111″). As for odd numbers (in our case 7), the (11+?) matches with “11″ initially but \1+$ cannot be true (notice the $) as there are 5 remaining ones. The regular expression engine willbacktrack and will make (11+?) match “111″ and here also \1+$ won’t be true because there will be 4 remaining ones (and \1+$ will only match with a number of ones which is a multiple of 3 followed by end of line) etc. hence “1111111″ will not match the regular expression which implies that 7 will be considered as being prime When I showed this to Christina this morning (true), she told me that this only checked for a number being odd or not. This is also what I felt at the beginning. But it really works. For instance, let’s try to apply it to 9 (which is obviously not even), “1″ * 9 should match the regular expression… “1″ * 9 = “111111111″. (11+?) matches “11″ initially. \1+$ cannot match because there are 7 remaining ones. Backtracking occurs. (11+?) now matches “111″. And here \1+$ matches the remaining 6 remaining ones! Hence, 9 is not prime. Easy… and beautiful at the same time Original from:http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/Avinash@noulakaz:~$ irb
irb(main):001:0> def is_prime(n)
irb(main):002:1> ("1" * n) !~ /^1?$|^(11+?)\1+$/
irb(main):003:1> end
=> nil
irb(main):004:0> is_prime(10)
=> false
irb(main):005:0> is_prime(11)
=> true
irb(main):006:0> is_prime(12)
=> false
irb(main):007:0> is_prime(13)
=> true
irb(main):008:0> is_prime(99)
=> false
irb(main):009:0> is_prime(100)
=> false
irb(main):010:0> is_prime(101)
=> true
and end of line ($) which implies that it matches either the empty string or “1″. This simply indicates that calling that function with n==0 or n==1 will correctly return false (as the “1″ * n will match with the first part of the regular expression)
A regular expression to check for prime numbers
20 Best Websites To Download Free EBooks
本文原文出自:20 Best Websites To Download Free EBooks, 译言翻译:20个免费下载各类电子书(Ebooks)的网站。 2.4eBooks Free-eBooks是一个以网络为收集来源的免费ebook下载网站,除了免费ebooks以外,您也下载免费杂志或上传您收集的ebook作为交流。 ManyBooks 提供PDA、iPod或者一般读者免费ebooks阅读与下载,这里有21,282 eBooks可利用,完全免费! GetFreeEBooks 是一个免费ebook下载网站,所有网站内的ebooks都是有经版权许可免费可使用的。 FreeComputerBooks 包括免费网络、编程、数学、技术、演讲笔记和教学的丰富资料库。分类按照标题,分成12个根目录和150个子目录。 FreeTechBooks 包括网络电脑科学,工程学和程序方面的书,课本还有演讲笔记,所有网站内的ebooks都是有经版权许可免费可使用的。 8.Scribd Scribd是线上文件分享网站,支援格式有:Word、Excel、PowerPoint、PDF与其他一些常用的格式,您可以下载或是贴在你的blog。详细介绍。 9.GlobuszGlobusz 是特別为发表ebooks设计的网站,专门研究免费eBook下载。文章与作者也以星等加以评价。 10.KnowFreeKnowFree免费交换ebooks、录影和其他材料,授权做为教育用途或是非商业用途。 11.OnlineFreeEBooksOnlineFreeEBooks 提供9大类的ebooks:汽车Ebooks,商业Ebooks、设计Ebooks、套件Ebooks、硬件Ebooks、健康与医疗Ebooks、软件与技术Ebooks,体育与武术Ebooks。 12.MemoWareMemoWare 有数以万计收藏(数据库、文学、地图、技术参考等等)特别的格式化的文件可以容易的传输到您的PalmOS、Pocket PC、Windows CE、EPOC、Symbian或其他PDA设备。 13.BluePortal 14.OnlineComputerBooksOnlineComputerBooks 包含关于免费电脑书 15.SnipFiles 16.BookYards The Online Books Page 超过30,000本免费ebooks。 AskSam Ebooks 有免费的收藏,如:莎士比亚、法律与政府文件。 Baen Free Library是下载科幻小说的在线图书馆。 20.eBookLobby 包括商务、艺术、电脑及教育。 21.Issuu Issuu是一个专业的在线PDF分享服务网站,或者也可以认为是一个很不错的在线PDF文档图书馆。详细介绍。 22.Yudu Yudu是一个在线PDF文档分享网网站,你可以发布自己的PDF文件,上面也有很丰富的PDF电子书资源。 23.Calameo Calameo同样是一个PDF文档分享网站,上面也有丰富的PDF文件可供下载。 Onsitecatalog也是一个在线PDF发布服务,上面同样有很多的免费的PDF文件资源。
1.FreeBookSpot
FreeBookSpot有4485本免费的E-BOOKS分成96个类别,多达71,97 GB。 您可以在类別搜寻找和下载免费的书,如:科学、设计、编码、小说和许多其他的书。您可以在类别搜寻和下载免费的书,如:科学、设计、编码、小说和许多其他的书。
4eBooks 有数量相当庞大的程式设计ebooks,下载的ebook都有简短的描述。您可以找到数以千计程式设计领域的ebooks,如:Net、Actionscript、Ajax、Apache..等等。
Using gnus In Windows with gmail's imap
1.Download stunnel installer and openssl
Alpine with gmail
Alpine with Gmail: 1. While installing, check the “Use local configuration file” option and click ok 2. In the “Alpine Configuration Setup” fill all the information with your full email address (user@gmail.com), imap.gmail.com and smtp.gmail.com 3. Launch Alpine -> Setup -> Config and enter the following: User ID: user@gmail.com User Domain: gmail.com smtp server: smtp.gmail.com:587/tls Inbox Path: imap.gmail.com/ssl/user=user@gmail.com