- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
If there was efficient code to do it, then any encryption based on the product of two large prime numbers would be easy to break.
Admin
Admin
of course you only check the prime numbers <= sqrt
Admin
Simplified EG: need to factorize: 35 Work out primes: 2 (known) 3%2=1 => 3 4%2 =0 no good 5%2, 5%3 != 0 => good Test 35/2 no good. 35/3 no good. 35/5 - winner winner, chicken dinner
Alternative;y, forget primes.... 35/2 no good 35/3 no good 35/4 no good 35/5 Winner, Winner, chicken dinner.
Maybe (sometimes) working out if a number is prime is overkill
Admin
AFAIK most pedants consider IIII more correct (as in that's what Julius Scissors would have used), but the convention these days is IV.
Admin
I'm off for some Almond Bread.
Admin
If we're counting the number of times something happens, it's useful to be able to reuse I when making II, then III then IIII. Otherwise we have to chuck our rock out quickly.
This is particularly true when keeping score in gladitorial sports (how many gladiators has this dude killed already). I guess it's kind of feasible that it was rare for someone to kill more than 3 before they get snuffed....
Admin
Admin
Admin
TRWTF is how did the prime candidate ever graduate from electoral college?
And that test of integrity, that should be illegal.
Admin
What I found interesting about this article was that I had written up two of my own prime-testing programs, one a brute force checker (from 1 to sqrt(number)) and one that only checked a list of already found prime numbers, and added to the list if the number was found prime. I was quite proud of the last one, giving it the ability to "learn" what numbers needed to be checked, but it ended up taking ~45 times longer than the brute force method(2050 seconds, about 34 minutes, as compared to 45 seconds for the brute-force check) to calculate what numbers up to 1,000,000 were prime.
Oh Well.
(note: I'm not a professional (read: I'm a complete amateur) writing in Java))
Admin
For things of a transitory nature Romans used wax tablets. For things that needed to be permanently recorded, they wrote them down on papyrus, parchment, or vellum. Romans highly respected and prized the ability to write.
Admin
Not 10 seconds after I hit "submit" I realised a way to drastically reduce the amount of time required for the second method. It now takes just under 3 times as long (174073 milliseconds as opposed to 62551 milliseconds. And yes I'm using real-time clocks, as I didn't need any big-O notation to realise it used to be crazy innefficient).
Admin
Many years ago, there were always students posting to various language groups on usenet. On comp.lang.c this problem came up quite often in the beginning of the school year. While the number of primes was usually those less than 1000, the given answer was always the same for the "can you give me a program" question. Yes, just a bunch of print statements with the numbers included.
Most likely the student realized:
This falls into the category of answering questions with the word "or" in them with "yes" (eg: Are you male or female?).
Admin
Hey, don't knock it. Obfuscated WTFs are way more fun to figure out. :)
Admin
Wrong Factorization is what RSA depends on, which is hard. Testing if a number is a prime fast is solved
http://en.wikipedia.org/wiki/AKS_primality_test
(in fact RSA relies on being able to test for primes very fast in order to select the two primes the key is made from)
Admin
It blows my mind that you can determine that a number is prime without knowing any of its factors. I need to brush up on my number theory.
I'm almost just as surprised that JRebel has found a pig large enough so that a single strip of bacon from it can completely encircle a Ferrari.
Man, everything is impressive today.
Admin
No wonder he was nervous...
-Harrow.
Admin
That said, IIRC (and it's a long time since I did anything involving RSA - and only ever in a school environment) the RSA problem is about taking two primes p,q and then multiplying (p-1)(q-1) rather than pq (pq is used, but I don't think known). What this means is that the problem is not quite as straight forward as factoring into primes.... eg: p=7, q=11 (p-1)(q-1) = 6*10 = 60 (2,3,4,5,6,10,12,15,20,30) all divide that. Of course, it's still rather advantageous to be able to know which numbers are prime, so that we can test prime-1.....
And I could be totally wrong....I'm no mathamagician.
Admin
Addendum (2012-11-09 01:00): Oh, and alternating the inc: inc = 6-inc
Admin
Admin
My "difference of squares" rule is what I use to calculate if numbers are prime in my head. With regards to mental arithmetic, I find squaring fairly simple and working out if a number is square fairly simple too.
Of course if you were using a computer program to do this you would be able to store a table of squares.
That mine had 19 as the first prime factor made it perhaps less efficient for this particular formula. It is generally more efficient when the number actually is prime, and when the prime factors are higher.
Take as another example 2773. Yes, I picked this number on purpose this time.
You go through your early checks but when you see the square root is just under 53, and work out that 53 is a valid "top" number to try for it, you will immediately arrive at the fact that 53 squared is 2809 which is 36 more than the target number, which means 2773 is 47 * 59.
Addendum (2012-11-09 04:49):
2773 is 5 mod 8, 1 mod 9, 3 mod 5, 1 mod 7. We're looking for numbers that are odd, 1 or 8 mod 9, 2 or 3 mod 5 and 1, 3, 4 or 6 mod 7.
53 satisfies all of those. Note the mod 9 rule eliminates more than any of the others here. 73 would have been the next eligible target, followed by 127.
Admin
Meh,
This is pretty typical of what I expect from developers really. Mainly ignorant and egotistical.
There are very few good developers.
Admin
Can you still blame BASIC for this nowadays? :-)
Admin
I was going to say something along those lines. Judging by his efforts he should be writing best practice guidelines for www.php.net
Admin
Dammit! All my code contains algorithms!
Admin
Admin
Primes is in P jackass. Look it up.
Admin
Wrong. What makes RSA secure is the difficulty of finding the factors of the product of two large primes. If it was hard to find large primes, then RSA wouldn't work at all, because you need to find two large random primes to create the keys.
Admin
Actually, TRWTF is not realizing that words sometimes morph over time to encompass very different meanings than what they originally were for. Especially when used in a slightly different industry.
Just one simple example: Bolt http://dictionary.reference.com/browse/bolt?s=t
Admin
I realise that the word is being used to mean something else, but it's really not applicable. The fold doesn't exist on a screen where a huge mix of resolutions means you can't ever know where it is. So sure, morph the word all you want, it's still wrong.
Admin
What word would you use? Google is failing me when it comes to finding a better one.
Admin
I wouldn't use any word because the very concept is faulty. It only came about because print designers were treating the web like print, which it isn't and never will be.
Admin
Nice try, but no.
Is it faster for the computer to extract the last digit and then test that for even/odd or divisible by 5, rather than testing the number as a whole? How are you going to extract the last digit? If you say, "Convert the number to a decimal string, then convert the string to a character array, then examine the last digit", big time fail. That would require multiple divisions by 10, creating new objects, etc etc. No ways that's going to be faster than just dividing by 2. Even if you think a little harder and say, okay, all we need to do to get the last digit is to take the number mod 10 ... well, how is
going to be faster than
You're doing two mods instead of one. I don't see how that helps.
Similarly, "only try numbers that end in 3, 7, 9, or 1". How do you find those numbers? You seem to think that
will be faster than
Moving two of the tests outside of the loop does nothing for run time. You still have to do the compares. No performance gain there. Plus it takes more code.
As to the rest, do you really think that doing 2 mods and 5 compares is faster than 1 mod and 1 compare? Why would that be true?
The lesson to be learned here is: Just because a shortcut works for a human being looking at decimal representations doesn't mean that it will be helpful for a computer looking at binary.
Admin
And furthermore, no concept from the print industry can or ever will be a valid analogy for a similar concept on the web?
That being the case, what word would you use for web content that is not immediately visible (ie without scrolling) after page load?
Not because the concept of "page load" has any meaning whatsoever in the print industry, obviously. Just curious.
Admin
Skipping even numbers is simple and saves you 1/2 of the execution time. Skipping numbers divisible by three only saves 1/3 of the remaining execution time. The payoff decreases with every prime you avoid this way, and complexity increases.
If you for example consider the primes up to 7, you get a cycle of length 210 of which you have to test 48 of them. You can precompute such a cycle of what length you prefer. I think by the time the length of the cycle would exceed the square root of the largest candidate to consider it no longer pays off. That means the length of the cycle needs to be less than the square root of the square root of the number. If the numbers are 64 bits, you'd reach the limit already when you have multiplied 2, 3, 5, 7, 11, and 13. Multiplying by 17 as well would give you a number which is larger than sqr(sqr(2^64)). I don't think the theoretical 16% savings from including 11 and 13 would be enough for me to bother implementing this algorithm.
Admin
If the concept is faulty, why does it work in practice? Comments at bottom of a page are simply not read as often as comments at the top of a page.
Admin
This is a prime comment http://thedailywtf.com/Comments/AddComment.aspx?ArticleId=7437
Admin
I was under the impression that factorizing prime numbers is exceedingly simple.
Admin
The closed form implementation is not equivalent in practice to the recursive implementation. Did your friend know that?
Admin
Yes, if you know they're prime.
Admin
Oversimplified just a bit. Yes, a PHP variable is a struct, but one member of the struct is a union (the item called "value", here).
Casting can't work that way exactly, because if you change the type, the content of "value" would also have to change, since these variables are overlaid and not separate primitives.
So if the types of two elements of an "==" compare are not of the same type then a cast would have to occur (explicit) or implicit and so your example at the end would happen more like this:
If you have one that's set up like so:
When you cast it to an int, you get this:
So a cast can't simply just change the type; it also has to set the correct member of the union, in order to ensure that the structure is correct for the new type.
I've done a little PHP, and I'm not sure it is a WTF in whole (though it certainly contains it's fair share of RWTF's, which isn't surprising given its history).
Admin
What content is that? Is that content viewed on a laptop screen, a large widescreen, a TV, a standard circa 90s 4:3 monitor? Just giving that unknown quantity of hidden content a name that means something different in the print industry does not make the issue of not knowing how much content is actually shown/hidden go away.
Admin
Nice strawman, but what you're saying is actually two different things. Things towards the top of the page are generally more important. But that has nothing to do with a "fold". In the past 3 years I've tracked over 550 different screen resolutions, and of those there's nearly 400 different heights for screen resolutions. Now tell me that there is a fold and that you know where it is.
Admin
If your target number is 1 mod 4 then you can also test if it is the sum of two squares.
If it is not the sum of two squares it is definitely NOT prime. However if it is, that is not a proof that it is prime.
e.g.
13, 17, 29, 37 and 41 are all the sum of two squares, but 21 and 33 are not.
(13=4+9, 17=1+16, 29=4+25, 37=1+36, 41=16+25)
25 is itself square as well as being 9+16 and 45 is 9+36, so as I said, being the sum of two squares is not a proof of primeness, but failing to be such is a clear test of non-primeness.
Assuming you have a table of squares less than your number you can thus eliminate some numbers without any divisions at all.
Note that if the number is 3 mod 4 it will never be the sum of two primes.
Admin
A comment is more important just because it happens to be #52 instead of #48? :headscratch:
Admin
Admin
Admin
Fast naive solution in python
def is_prime(n): return n >= 2 and all(map(lambda d: n % d, range(2, int(n ** 0.5) + 1)))
def primes(n): i = 2 while i < n: if is_prime(i): yield i i += 1
Admin
Factoring can be done trivially in time equal to the square root of the number to be factored. The factors are efficient, it's just that numbers can be arbitrarily large.