Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
If Kerouac wrote JavaScript (anguscroll.com)
66 points by mrcoffee on July 9, 2013 | hide | past | favorite | 9 comments


Faulkner:

    function factorial(n) {
        return "a fish";
    }


I especially like the Feynman solution. Sometimes it doesn't have to be elegant to be correct.

"If I could explain it to the average person, I wouldn't have been worth the Nobel Prize." - Feynman, 1985


And yet, his book on the subject, published that year [QED: The Strange Theory of Light and Matter] does about as good a job explaining it to the average person as humanly possible. From the intro to the 2006 edition: "According to Feynman, to learn QED you have two choices: you can go through seven years of physics education or read this book".


George R.R. Martin:

    function factorial(n) {
        delete n;
    }


I wish I could upvote this one a few times... proof that JS is an elegant and expressive language to say the least.


somebody needs to take a crack at a David Foster Wallace version ...


  // And but so then and this is, insofar as what a brief perusing glance of the source code printed below affords, this code would seem to be a recursive algorithm that calculates a factorial number[1], not that reading such code is beyond the average liberal arts grad and I wouldn't want to to give the impression I'm assuming your ignorance in selecting such a simple example to illustrate my point but so to get on with what I'm demonstrating, here's this little snippet of code I found lying around that's a fairly common introduction to a variety of different programming languages, which by the way is for purposes of brevity and simplicity as well as demonstrating the concept of recursion to new programmers uses a rather inefficient method with a running time of O(n^2) where fast multiplication algorithms such as Schönhage–Strassen[2] algorithm are proven to run in O(n(log n log log n)^2) time, not that this really matters since a factorial is going to overflow a 64 bit float pretty quickly so there's not that many steps you can perform before you've overstepped the bounds of what can be usefully calculated with this algorithm and you'd have to overhaul it to support some sort of custom-built bigint object but still it's worth mentioning so and here's this code:
  function factorial(n)
  {
    if (n <= 1) return 1;
    return n*factorial(n-1);
  }
  
  ...
  
  // Footnotes
  // [1] In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. The value of 0! is 1, according to the convention for an empty product.[1] The factorial operation is encountered in many areas of mathematics, notably in combinatorics, algebra and mathematical analysis. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence (i.e., permutations of the set of objects). This fact was known at least as early as the 12th century, to Indian scholars.[2] The notation n! was introduced by Christian Kramp in 1808.[3] The definition of the factorial function can also be extended to non-integer arguments, while retaining its most important properties; this involves more advanced mathematics, notably techniques from mathematical analysis. Although the factorial function has its roots in combinatorics, formulas involving factorials occur in many areas of mathematics. There are n! different ways of arranging n distinct objects into a sequence, the permutations of those objects. Often factorials appear in the denominator of a formula to account for the fact that ordering is to be ignored. A classical example is counting k-combinations.
  // [2] The Schönhage–Strassen algorithm is an asymptotically fast multiplication algorithm for large integers. It was developed by Arnold Schönhage and Volker Strassen in 1971.[1] The run-time bit complexity is, in Big O notation, O(N log N log log N). The algorithm uses recursive Fast Fourier transforms in rings with 22n + 1 elements, a specific type of number theoretic transform. The Schönhage–Strassen algorithm was the asymptotically fastest multiplication method known from 1971 until 2007, when a new method, Fürer's algorithm, was announced with lower asymptotic complexity;[2] however, Fürer's algorithm currently only achieves an advantage for astronomically large values and is not used in practice. In practice the Schönhage–Strassen algorithm starts to outperform older methods such as Karatsuba and Toom–Cook multiplication for numbers beyond 2215 to 2217 (10,000 to 40,000 decimal digits).[3][4][5] The GNU Multi-Precision Library uses it for values of at least 1728 to 7808 64-bit words (33,000 to 150,000 decimal digits), depending on architecture.[6] There is a Java implementation of Schönhage–Strassen which uses it above 74,000 decimal digits.[7] Applications of the Schönhage–Strassen algorithm include mathematical empiricism, such as the Great Internet Mersenne Prime Search and computing approximations of π, as well as practical applications such as Lenstra elliptic curve factorization, in which multiplication of polynomials with integer coefficients can be efficiently reduced to large integer multiplication.


I think you've won "longest horizontal scrolling on HN"


Brilliant!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: