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 2011-04-05 15:33

    ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
    █     ___                        █
    █    //  7                       █
    █   (_,_/\       WORSHIP THIS    █
    █    \    \   YOUR THROBBING GOD █
    █     \    \                     █
    █     _\    \__                  █
    █    (   \     )                 █
    █     \___\___/                  █
    █                                █
    ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

Name: Anonymous 2011-04-05 15:33

    ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
    █     ___                        █
    █    //  7                       █
    █   (_,_/\       WORSHIP THIS    █
    █    \    \   YOUR THROBBING GOD █
    █     \    \                     █
    █     _\    \__                  █
    █    (   \     )                 █
    █     \___\___/                  █
    █                                █
    ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

Name: Anonymous 2011-04-05 15:33

    ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
    █     ___                        █
    █    //  7                       █
    █   (_,_/\       WORSHIP THIS    █
    █    \    \   YOUR THROBBING GOD █
    █     \    \                     █
    █     _\    \__                  █
    █    (   \     )                 █
    █     \___\___/                  █
    █                                █
    ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

Name: Anonymous 2011-04-05 15:33

    ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
    █     ___                        █
    █    //  7                       █
    █   (_,_/\       WORSHIP THIS    █
    █    \    \   YOUR THROBBING GOD █
    █     \    \                     █
    █     _\    \__                  █
    █    (   \     )                 █
    █     \___\___/                  █
    █                                █
    ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

Name: Anonymous 2011-04-05 15:36

    ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
    █     ___                        █
    █    //  7                       █
    █   (_,_/\       WORSHIP THIS    █
    █    \    \   YOUR THROBBING GOD █
    █     \    \                     █
    █     _\    \__                  █
    █    (   \     )                 █
    █     \___\___/                  █
    █                                █
    ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

Name: Anonymous 2011-04-05 15:36

>>39-44
This is why I hate little kids.

Name: PUDDI 2011-04-05 16:07

PUDDI

Name: PUDDI 2011-04-05 17:02

>>46
fuck off and die faggot

Name: Anonymous 2011-04-05 17:10

>>47-48
Summer is near.

Name: Anonymous 2011-04-05 17:29

>>49
I think I'm going to run the Anusbot for three months non-stop.

Name: Anonymous 2011-04-05 17:32

>>50
How about complaining about the current status of /prog/ with MrVacBob-sama-kun-san? It doesn't work.

Name: Anonymous 2011-04-05 22:53

>>50
lolol progfag y u dont just go fuck urself cuz noone else is going2 lmao

Name: Anonymous 2011-04-06 0:08

>>50-51
ur lisp move but all i can hear is "im a huge faggot please rape my face" -u

Name: Anonymous 2011-04-06 9:50

One line.  I win.

[code]
#include <iostream>

main() {cout << "402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";}

Name: Anonymous 2011-04-06 10:13

Can you write one that does this within 1000! lines of brainfuck?

Name: Anonymous 2011-04-06 10:50

>>55
you fucked up your post, check your semantics

Name: Anonymous 2011-04-06 11:29

>>55
402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 (at least) increment operation will take quite a while

Name: Anonymous 2011-04-06 11:30

>>55
(make-string (fact 1000) #\+)

Name: Anonymous 2011-04-06 11:40

>>56

I don't see where I made an error.

Name: Leonidas 2011-04-06 11:42

lol, retards

4 lines in java

import java.math.BigInteger;
public class Factorial {
    public static void main(String[] args) {  
        //-- BigInteger solution.
        BigInteger n = BigInteger.ONE;
        for (int i=1; i<=1000; i++) {
            n = n.multiply(BigInteger.valueOf(i));
        }
        System.out.println(n);   
    }
}

Name: Anonymous 2011-04-06 11:43

>>60

3 lines actually

BigInteger n = BigInteger.ONE;
for (int i=1; i<=1000; i++) {
n = n.multiply(BigInteger.valueOf(i));

Name: Anonymous 2011-04-06 11:43

Name: Anonymous 2011-04-06 12:21

>>60
lol retard, one line in <insert name of one of bazillions languages>

Name: Anonymous 2011-04-06 15:32

print reduce(lambda a, b: a * b, range(1, 1000))

putStrLn $ show $ product [1..1000]

(1..1000).reduce(:*)

lists:foldl(fun(A, B) -> A * B end, 1, lists:seq(1, 1000))

Name: Anonymous 2011-04-06 16:15


factorialere sic
  da meo numerum parprimum.
  unum meo resulto da.
  si numerum tum nullum aequalitam fac unum redde.
  dum numerum fac sic
   resulto damentum numerum tum numerum unum deme multiplica.
   numerum damentum numerum unum deme.
  cis
  numerum redde.
cis

Name: Anonymous 2011-04-06 21:25

>>39
>>40
ok

Name: Anonymous 2011-04-07 12:00

>>64
Python, Haskell, Perl and what is the last one?

Name: Anonymous 2011-04-07 12:16

>>67
Looks like Erlang.

Name: Anonymous 2011-04-07 13:41

>>64
.reduce(:*)
(:*)
:*

Name: Anonymous 2011-04-07 13:48

>>65
Is that some Perl?

Name: Anonymous 2011-04-07 14:12

>>70
It's valid Perl code, yes.

Name: Anonymous 2011-04-07 18:34

>>69
69
(* ) ( *)


inb4lrn2quote Shut the fuck up. It's two [b][i]SEPARATE[i][/b] quotes!

Name: Anonymous 2011-04-07 18:37

>>72
lrn2bbcode

Name: >>72 2011-04-07 23:20

>>73
Oh shit, I posted it and I only saw it now.

It's such a shame. Let me fix it:
It's two SEPARATE quotes!

Name: Anonymous 2011-08-31 15:56

>>13

(define small-odd-swing
  (vector 1 1 1 3 3 15 5 35 35 315 63 693 231 3003 429 6435 6435 109395 12155 230945 46189 969969 88179 2028117 676039 16900975 1300075 35102025 5014575 145422675 9694845 300540195 300540195))


WTF is this? This is what I found:
``Odd part of the swinging factorial''
http://oeis.org/A163590
http://www.luschny.de/math/swing/SwingingFactorial.html

How do you use this sequence to speed up the computation of factorial 1000? Care to explain?

Name: n3n7i 2011-09-01 1:16

Done in 3 sec using Thousandfold + Binary Alg =)

285 places on Base 10^9....

Name: Anonymous 2011-09-01 1:33

>>1
I can do it in one line of C, assuming I get to include some sort of bignum math library.  I don't see why I shouldn't, since Haskell does.

Name: Anonymous 2011-09-01 1:40

>>77
Ah, wait, I don't even need the bignum library:

#include <stdlib.h>
int main(int argc, char **argv) { return system("start http://www.wolframalpha.com/input/?i=1000!"); }

Ok, two lines of code but the #include isn't usually considered a line of code.

Name: Anonymous 2011-09-01 2:02

>>78
Or you could call Haskell from C, which would be much faster than requesting wolframalpha over the network.

Name: n3n7i 2011-09-01 2:16

!1500 in 5 sec =)

It looks like this
48,119977967,797748601,669900935,813797818,348080406,726138081,308559411,630575189,1095591,292230585,206733851,868464009,619343585,194052091,124618166,270271481,881393331,431627962,810299844,149333789,44689395,510487167,879769325,303699470,467829234,399263326,545652860,748605075,746366928,323606645,492277541,120083438,86727369,377887676,211405,318480244,354207419,604864176,969950581,435222198,851194568,984095705,945549589,54568321,792338919,149442985,919957734,792959402,499096845,643020401,869381175,603964424,333222114,125974374,817804242,633309769,804293952,870034619,354125014,210045647,664063240,162007560,108665290,568646128,342557147,350985358,724154623,253371867,470765120,422073867,963935775,258692109,753041762,94343569,50497470,353531764,481503174,750911858,230906998,361066084,787758316,110585736,13365377,431860738,572261325,738233656,835271947,352695180,865573043,834027955,539012765,489372645,42504406,597752357,481931532,872356635,411224578,334040522,294746402,829585458,478708778,346379431,862368824,819009177,91444034,885941394,319343910,223168655,869761799,669075059,527608502,465593181,398566214,786801211,651657222,4123456,498258513,120359126,22843038,535083709,796101565,934859483,203933443,308601475,813108363,74118562,404412420,191947127,585482919,172173045,961122122,701434297,870691932,154082986,945954748,251105782,181586397,275820342,101470457,300633590,139512919,549474113,721711616,912519714,191760699,935509810,254849967,87635936,181176363,954224186,31346682,928878492,872249485,456690138,831610135,377916327,940503701,400290125,509132140,782614640,495733518,48670983,360134097,860364762,638658894,873174499,870133559,364805443,430831459,505987809,215393353,387232078,177562975,21460595,422358573,128085417,162336030,235138652,735438053,34531962,620811566,19896879,275257163,988352090,874930346,115518331,202927263,708446729,394381879,888839549,731876978,682249320,628599631,628662375,508826209,854754631,984276392,670919216,923002770,77734756,77549035,942976209,159416211,581439461,484509549,370357486,770276807,687544580,164314647,595031368,948490282,897173328,13518435,758700056,425922638,411889496,527975846,52717958,44813737,86806600,171993703,579485864,29383208,714528950,303253881,360812631,162134750,100307772,634337467,12820470,715650810,714689905,121432259,528505483,53930402,217400686,61612471,659630192,434864094,539828085,677465383,26128353,771071152,304197549,798870706,139893609,140045659,756285435,787771636,258253666,592102151,236142132,724425850,991205720,20493660,580896600,891888594,659612927,724357866,265934517,615841298,789154462,249169688,860092640,284756382,431746120,357767933,119589280,468687348,61788072,986362788,582227019,465263474,828590646,48451070,702923434,422714349,595857654,843699542,321849363,652767771,978314681,13589442,955219879,702008068,934096624,650625769,705233333,462826013,860098698,155180331,145365652,453482955,497979915,586438474,687345677,874451117,702250441,711504844,638414485,210092261,397271970,571029038,581873069,951161330,495772310,508760528,249706514,238384269,808639507,80418298,318311361,373628512,41716415,196868334,254119137,139589149,597210032,153545941,114666530,498906529,240798164,804007394,775927836,45668573,993316428,972539932,745757171,947402454,257142633,700815922,407278403,640595355,142075599,446056337,986717212,316223257,763412164,180899532,722039383,244462511,410346646,148863397,237096276,822656157,561194665,545757017,429842404,840309758,925618650,507921043,7241637,877939825,811059339,138925526,124514467,627126548,126795078,784022672,860886251,974581362,141782786,407402896,309678008,909663263,987018538,107050886,193489012,497405005,820727271,232733728,141775132,722013860,591169620,692789290,456794698,409808557,447756701,311883266,10859016,27592252,397754508,251628808,293537776,536569608,111330584,797160694,847898923,196743970,244451842,702266403,326317319,92117151,143971679,500042590,269255093,130215984,418097418,435474300,467281949,798227102,529873732,749027992,79700287,275900856,241172902,880909546,551703263,202853584,498085358,955307673,717177961,902081098,618729046,348849060,249600000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

10 ^ 9 * 457

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