Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

1000 factorial

Name: Anonymous 2011-04-02 22:59

Well, someone wrote that they could write a program to do 1000! in one line of Haskell. I said that it would take less than 100 lines of C to do the same. I wrote the program, and it is less than 100 lines of C:

#include <stdio.h>
#include <string.h>

#define LENGTH 10000

int max (int a, int b) { return (a > b) ? a : b; }
int min (int a, int b) { return (b > a) ? a : b; }

int length (unsigned char * a)
 {
   int i;
   i = LENGTH - 1;
   while ((a[i] == 0) && (i >= 0)) i--;
   return i + 1;
 }

void add (unsigned char * a, unsigned char * b)
 {
   int i, c, C;
   c = min(max(length(a), length(b)) + 1, LENGTH);
   for (i = 0, C = 0; i < c; i++)
    {
      a[i] += b[i] + C;
      C = a[i] > 99;
      if (C) a[i] -= 100;
    }
 }

   unsigned char x [LENGTH], y [LENGTH];
void mul (unsigned char * a, unsigned char * b)
 {
   int i, j, c, d, C;
   memset(y, '\0', LENGTH);
   c = min(length(a) + length(b) + 1, LENGTH);
   d = length(b);
   for (i = 0; i < d; i++)
    {
      memset(x, '\0', LENGTH);
      for (j = 0, C = 0; (i + j) < c; j++)
       {
         x[i + j] = (a[j] * b[i] + C) % 100;
         C = (a[j] * b[i] + C) / 100;
       }
      add(y, x);
    }
   memcpy(a, y, LENGTH);
 }

void numset (unsigned char * a, unsigned long b)
 {
   int i;
   for (i = 0; i < LENGTH; i++)
    {
      a[i] = b % 100;
      b /= 100;
    }
 }

int tz (unsigned char * a)
 {
   int i;
   for (i = 0; i < LENGTH; i++) if (a[i] != 0) return 0;
   return 1;
 }

   unsigned char minus1 [LENGTH], prod [LENGTH];
void fact (unsigned char * a)
 {
   numset(prod, 1);
   while (!tz(a))
    {
      mul(prod, a);
      add(a, minus1);
    }
   memcpy(a, prod, LENGTH);
 }

   unsigned char arg_res [LENGTH];
int main (void)
 {
   unsigned long a;
   int i;
   memset (minus1, 99, LENGTH);
   printf("Input the number to take the factorial of: ");
   scanf("%lu", &a);
   numset(arg_res, a);
   fact(arg_res);
   i = LENGTH - 1;
   while (arg_res[i] == 0) i--;
   while (i >= 0)
    {
      printf("%02d", (int) arg_res[i]);
      i--;
    }
   putchar('\n');
   return 0;
 }

Name: Anonymous 2012-07-21 20:56

Prolog was made in France, and France is fucking shit.

Go fuck an autistic ``le parfum d' snail".

Name: Anonymous 2012-07-21 23:17

I said that you should design rules that work both ways, since it's Prolog and end users kind of expect usability.
Why isn't is/2 designed that way?

Name: Anonymous 2012-07-21 23:32

>>162
I'd gather that backtracking through the full numeric tower would be too much of an academic masturbation even for Prolog. We have set theory for that.

Name: Anonymous 2012-07-21 23:59

too much of an academic masturbation even for Prolog
I seriously doubt such a thing is possible.

Name: Anonymous 2012-07-22 0:26

>>162
Arithmetic in Prolog is basically just a hack, you can't always invert complex mathematical operations (let alone do it efficiently) so you would end up with what >>163 says unless you weren't extremely careful about it. Prolog was designed to work on real computers after all.

This doesn't mean that you should design stupid and unusable shit with it though.

Name: Anonymous 2012-07-22 2:43

Fuck it, did it with a bignum library in Go while high last night, come at me.


package main

import (
    "math/big"
    "fmt"
)

func main() {
    sum := big.NewInt(1)
    for i := int64(1000); i > 1; i-- {
        sum.Mul(sum, big.NewInt(i))
    }
    fmt.Println(sum)
}

Name: Anonymous 2012-07-22 3:33

Would anyone care to show me how to use numeric transform to do multiplication in O(n log n)?

Name: Anonymous 2012-07-22 3:34

fuck I meant number-theoretic

Name: PUDDI 2012-07-22 4:16

>>46
puddi -c 'print reduce(lambda x,y:x*y,xrange(1,1000))'

Name: PUDDI 2012-07-22 4:16

>>46 fuck
puddi -c 'print reduce(lambda x,y:x*y,xrange(1,1001))'

Name: Anonymous 2012-07-22 6:05

>>166
Holy fuck, with a bignum library? Are you crazy? That's hardcore.

Name: Anonymous 2012-08-03 19:02


in ruby

(0..1000).reduce(:*)

ermagawsh that was difficult
will someone shut these faggots up please

Name: Anonymous 2012-08-03 19:24

>>172
How many centuries does that take to run?

Name: Anonymous 2012-08-03 19:35

>>173
I'd like to tell you, but the estimator I wrote is still running.

Name: Anonymous 2012-08-03 20:20

>>172
wrong output

Name: Anonymous 2012-08-03 20:29

>>175
ruby programmers users can't into programming.

Name: Anonymous 2012-08-03 21:36

>>172
0 * 1 * 2 * ... * 1000 = ?

Name: Anonymous 2012-08-03 21:53

[*]-<<+^<<^1000
VALID PERL 6 CODE

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List