7 ms·
Computing primes with CSS
- andrewclunn 10y agoBut would that be... as stylish? ##### #### _\_ ________ ##=-[.].]| \ \ #( _\ | |------| # __| | |||||||| \ _/ | |||||||| .--'--'-. | | ____ | / __ `|__|[o__o]| _(____nm_______ /____\____
- sctb 10y agoWe detached this comment from https://news.ycombinator.com/item?id=12611579 https://news.ycombinator.com/item?id=12611579 and marked it off-topic.
- vurpo 10y ago"Your scientists were so preoccupied with whether they could that they didn't stop to think if they should." --Dr. Ian Malcolm (Jurassic Park)
- ManlyBread 10y agoWe have clearly passed the point where we should stop, but let's keep going and see what happens.
- deleted 10y ago[deleted]
- daptaq 10y ago-- Scientist from Jurassic Park
- kafkaesq 10y agoHe already knows very well that he "shouldn't have". The point is he did it anyway.
- clifanatic 10y agoNature finds a way.
- vortico 10y agoYou could replace the "+4" in "2n+4" with "+0" to remove the amount of custom text you have to insert into each line.
- parceltape 10y agobut then 2 would be hidden, and 2 is prime.
- yesbabyyes 10y agoNo, the point is to display the primes (2, 3, 5, 7...).
- deleted 10y ago[deleted]
- amelius 10y agoHow does the size of the code increase with the number of primes shown? Imho, the code should not grow. So in my view, the article is cheating.
- mikejb 10y agoCode size increases in Θ(n^(0.5)) I agree that the title is somewhat misleading - it is (to me) as much a 'computation of prime numbers' as a lookup table would be.
- orf 10y agoWouldn't a bit of js to create them on the fly stop that? It would still be a kind of lookup table though.
- mikejb 10y agoI think using JavaScript would move the concept even further away from "computing primes with CSS". It would just do some things on the fly that the author did with python.
- bonzini 10y agoFWIW, this[1] is closer to how a "real" sieve of Eratosthenes program might look like, with three optimizations: 1) omitting even numbers from the sieve, 2) sieving starting at n^2, 3) precomputing primes up to sqrt(n) if you want to sieve up to n. The code size then is Θ(sqrt(pi(n)). [1] https://jsfiddle.net/L8mxuag5/1/ https://jsfiddle.net/L8mxuag5/1/
- contravariant 10y agoWell it's closer to O((n/log(n))^0.5), although that doesn't make much of a difference until n becomes rather large.
- mikejb 10y agoGood catch - I didn't differentiation between n as 'upper limit for primes' and n as 'input length'. The former is more intuitive, which makes your solution more accurate. (Also, I should have played it safe and use O instead of Θ...)
- XCSme 10y agoYou can do the same thing with pure HTML, just write `2,3,5,7,11,13...`. I am disappointed by the article, was expecting something with CSS `calc()`.
- deleted 10y ago[deleted]
- startling 10y agoThat's not similar, at all. This is actually removing multiples, as in the sieve of erastosthenes.
- rootlocus 10y agoIt's missing a loop, and the primes are hardcoded into the source code (see the similarity?). You might be mislead by the fact that you also find 'span:nth-child(4n+8)' or 'span:nth-child(6n+12)' (which aren't primes) but those are redundant, and an efficient sieve would skip non primes.
- nedbat 10y agoThe point is that the file has nothing about the primes in it. 6 and 7 are treated equally, but 7 is displayed and 6 is not.
- rootlocus 10y ago
- 0xmohit 10y agoI once saw an interesting way of computing primes using LaTeX [0]. The link also mentions a way of doing the same in plain TeX that is taken from Donald Knuth's "The TeXbook". [0] http://tex.stackexchange.com/questions/134305/how-to-produce-a-list-of-prime-numbers-in-latex http://tex.stackexchange.com/questions/134305/how-to-produce...
- qwertyuiop924 10y agoWell, TeX is supposed to be able to do that, which makes it less cool.
- deleted 10y ago[deleted]
- wanda 10y agoThis is way cooler: http://silon.slaks.net http://silon.slaks.net
- eriknstr 10y agoThat's not just cool, that's crazy!
- adeel4 10y agowait, what?!
- aji 10y agois it, though? https://github.com/SLaks/Silon#implementation-details https://github.com/SLaks/Silon#implementation-details "Silon is implemented using LESS to generate CSS selectors for every combination of inputs that will turn each gate on. In effect, I created a LESS-based DSL that expresses (almost-)arbitrary boolean expressions."
- wanda 10y agoYes, I know a preprocessor is involved, much like many single-element/background-image hacks and CSS-only glitch effects which translate to fifty-keyframe animations. It's still cooler than the OP.
- aji 10y agoit's definitely shinier than the OP, but I personally think encoding a sieve of Eratosthenes in a CSS selector is cooler than encoding truth tables in a CSS selector
- wanda 10y agoI was always more interested in the philosophy of mathematics than prime numbers. Actually, I'm interested in philosophy in general, especially early Wittgenstein & Gödel. So I'm biased.
- 10y ago
- jonatanheyman 10y agoI made a slightly improved version using <ol> and <li> elements to not have to manually output the numbers within the <span> element: https://jsfiddle.net/r8c2Lsz7/ https://jsfiddle.net/r8c2Lsz7/
- nedbat 10y agoNice! :)
- co_dh 10y agoYou can remove all non primes in the selector, like below span:nth-child(2n+4), span:nth-child(3n+6), span:nth-child(5n+10), span:nth-child(7n+14), span:nth-child(11n+22), span:nth-child(13n+26), span:nth-child(17n+34), span:nth-child(19n+38), span:nth-child(23n+46), span:nth-child(29n+58), span:nth-child(31n+62),
- kafkaesq 10y agoBut that'd be cheating. How do you know what the primes are before you've computed them?
- jaywunder 10y agoI don't understand why the commenters in this thread are being so negative towards this project. This is a fun experiment to see what the author could do with the minimal tools CSS provides. Sure there's no use for this kind of project. Not everything has to have a use. Some things can be for pure fun. The negativity in this thread isn't even productive or helpful.
- crdoconnor 10y agoI can't speak for the others but I'm very wary of "too powerful" languages and language features. They lead naturally to technical debt and security vulnerabilities. This sort of explains how I feel: http://lukeplant.me.uk/blog/posts/less-powerful-languages/ http://lukeplant.me.uk/blog/posts/less-powerful-languages/ That's why my reactionary instinct to "neat hacks" like this is usually one of revulsion (at the language for making it possible).
- rootlocus 10y agoThe negativity probably comes from the fact that people expect to find high quality content on the HN front page. Pet projects like these are not impressive enough to be featured.
- talmand 10y agoSays you.
- mikejb 10y agoI don't feel really negatively towards this project, but after reading the title and looking at the code it felt a bit disappointing. When I read "computing primes" I didn't expect so much pre-computation cooked into the code.
- kafkaesq 10y agoIt's what you might call a "halfway algorithm" -- yes it needs some major fudging baked in (because from what I understand you can't do things iteratively in CSS). But it does, intrinsically, compute the primes. And the point is it demonstrates a kind of computation one normally wouldn't think was even possible with CSS (that is: not that you'd rule it out as beyond the computational reach of CSS; but it's definitely the sort of computation you normally wouldn't think to do with CSS). So I'd say it's definitely a valid hack.
- partycoder 10y agoThat's because CSS is turing complete. The rule 110 cellular automaton has been implemented in CSS, and because the rule 110 is turing complete, that means CSS is turing complete.
- minitech 10y agoWhat’s “because” CSS is Turing complete? Anyway, this implementation could be done using a DFA (because it’s not actually computing primes with CSS).
- carapace 10y agoUnrelated to the main story, those damnable f-strings are awful. For goodness' sake, don't put code into your format strings, you maniacs! But don't take my word for it: http://www.stringtemplate.org/about.html http://www.stringtemplate.org/about.html
- nedbat 10y agoAnything can be overused. Do you find my use of them "awful"? It will mean we don't need code like: "...".format(a=a, b=b, c=c, d=d)
- carapace 10y agoNo, I will admit your usage here seems perfectly cromulent. ;-) (Don't pay too much attention to me: I still haven't adopted the .format() method! Heh.) Very nice project sir!
- mixmastamyk 10y agoYour use there is exactly what it was designed for. I've enjoyed your work over the years, so I'm quite happy you found it useful.