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 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;
}
here it OP, also a one-liner
printf("402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
Name:
Anonymous2011-04-03 2:11
New challenge: Write the shortest C program that outputs the factorial of 1000.
>>14
Yes it is.
; just for reference, Π is:
#;
(define (Π f n m #:threads (c (processor-count)))
(let* ((t (lambda (a)
(lambda ()
(let/ec k
(do ((x (+ n a) (+ x c))
(r 1 (* r (f x))))
((> x m) (k r)))))))
(xs (do ((x (sub1 c) (sub1 x))
(xs '() (cons (future (t x)) xs)))
((zero? x) xs)))
(x ((t 0))))
(foldr * x (map touch xs))))
>>10 doesn't seem to know the difference between C and Python.
>>11-12 seems to be confusing programming languages as well.
>>13,15 is an expert Scheme programer, but doesn't seem to know the difference between C and Scheme. I'd give it the most interesting implementation award, however.
I'm calling it right now, you guys are fucking morons, it wasn't a response to your retarded little SEE challenge, it was a response to the original post. It even runs faster than your pathetic entries, if you're writing C at least do it right.
>>20 >>1 is good enough, without using a bignum library. We'd just reimplement it over and over. I'd say this should be a ``make a fast/complete C bignum library'' challenge.
>>16
OP here. Nice. The math is problem specific, and faster for it. >>21
Also, nice. You took a language which you wrote factorial in one line, and wrote it in alot of lines, and showed that version to be superior. How the hell does it work?
You have to be smarter than the language. How many C programmers can do ad hoc bignum math that does more than addition and subtraction? How many Scheme programmers can write >>13 ?
>>25 >>21
I'm not the deluded pythonista! I'm usually respectful, and knew that >>13 is not a valid entry.
I tried to do it in C, and wanted to make the bignum part faster, smaller (using a whole uintmax) and general, but it seems I can't write C code anymore. I give up, it would be much easier in Assembly.
What I've done so far:
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
void bignum_addbi(bignum *n, uintmax_t m) {
size_t i, j = n->len;
uintmax_t a;
for (a = n->val[i], i = 0; m && i < j; i++) {
a = n->val[i];
n->val[i] += m;
m = a > n->val[i];
}
if (m && i >= n->len)
bignum_realloc(n, n->len+1, 1);
}
void bignum_addbb(bignum *n, bignum *m) {
size_t i, j = max(n->len, m->len);
uintmax_t a; int c;
if (n->len < m->len)
bignum_realloc(n, m->len, 0);
for (i = 0, c = 0; i < j; i++) {
a = n->val[i];
n->val[i] += m->val[i] + c;
c = a > n->val[i];
}
if (c && i >= j++)
bignum_realloc(n, j, 1);
}
void bignum_subbi(bignum *n, uintmax_t m) {
size_t i, j = n->len;
uintmax_t a;
for (a = n->val[i], i = 0; m && i < j; i++) {
a = n->val[i];
n->val[i] -= m;
m = a < n->val[i];
}
if (--j && !n->val[j])
bignum_realloc(n, j, n->val[j]);
}
void bignum_mulbb(bignum *n, bignum *m) {
if (bignum_zerop(m)) {
bignum_dealloc(n);
n = bignum_from_uintmax(0);
}
if (bignum_equalpi(m, 1)) return;
bignum *i = bignum_copy(m);
bignum *l = bignum_copy(n);
for (; !bignum_equalpi(i, 1); bignum_subbi(i, 1))
bignum_addbb(n, l);
bignum_dealloc(i);
bignum_dealloc(l);
}
void bignum_mulbi(bignum *n, uintmax_t m) {
if (!m) {
bignum_dealloc(n);
n = bignum_from_uintmax(0);
}
if (m == 1) return;
bignum *l = bignum_copy(n);
while (--m)
bignum_addbb(n, l);
}
int bignum_zerop(bignum *n) {
return (!(!(n->len-1) && n->val[0]));
}
int bignum_equalp(bignum *n, bignum *m) {
if (n->len != m->len) return 0;
size_t i = n->len;
while (n->val[i] == m->val[i]) i--;
return !!i;
}
inline int bignum_equalpi(bignum *n, uintmax_t m) {
return (!(n->len > 1)) && (n->val[0] == m);
}
void debug_print_bn(bignum *n) {
printf("Memory Location: %#08x; %#08x\n", n, n->val);
printf("Length: %u\n", n->len);
size_t i;
for (i = 0; i < n->len; i++)
printf("Cell#%08u: %016llx\n", i, n->val[i]);
}
Name:
Anonymous2011-04-04 1:29
>>26
OVER 9000 REALLOC OPERATION
REWRITE THAT SHIT
Name:
Anonymous2011-04-04 1:39
Or the weenie can learn to write proper code using a functional programming language like Haskell.
>>26
Is it me, or does that look like GNU bc code? >>21
You can call me a fucking retard when you post a portable multithreaded FFT multiplication implementation that's one shot for a factorial program. I'm not a retard just because Scheme holds your little hand and hides the machine from you.
Name:
Anonymous2011-04-04 13:18
>>31
Shit, am I that bad? I've never seen GNU bc's code.
>>21
Scheme
I am the Schemer, that was the deluded pythonista troll. I'm aware I suck at C.
>>36
It does prime factorization, and basically is a faster version of
(define (factor x p)
(let loop ((x (quotient x p))
(r 0))
(if (< x 2) (+ x r)
(loop (quotient x p)
(+ x r)))))
I'm not exactly sure of how swing exactly works, and the rec-fact thing seems to be there to make it run in logarithmic time. The bit-count thing is a mystery too.
I'm still trying to understand it too.
Can you write one that does this within 1000! lines of brainfuck?
Name:
Anonymous2011-04-06 10:50
>>55
you fucked up your post, check your semantics
Name:
Anonymous2011-04-06 11:29
>>55 402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 (at least) increment operation will take quite a while
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
>>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.
>>78
Or you could call Haskell from C, which would be much faster than requesting wolframalpha over the network.
Name:
n3n7i2011-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
Name:
Anonymous2011-09-01 2:43
go fuck an autistic nigger, you fucking assburgers fagstorm shitbreath pencildick nigfucker
Hey C fags when you include a header the amount of lines in your program actually increases, if it didn't you could just write everything under one big macro include a header and do EVERYTHING as a one liner, you have to explicitly use extern or your code is invalid and the amount of lines used compiler/system/architecture dependent.
Name:
Anonymous2011-09-01 9:38
@Anonymous
No, those are standard library functions. Just like "alert" in javascript. The calculations are not done by iostream.
Name:
Anonymous2011-09-01 9:39
@Anonymous
No, those are standard library functions. Just like "alert" in javascript. The calculations are not done by iostream.
Name:
Anonymous2011-09-01 9:40
Sorry for double post. Seems like double click is double post on my browser...
I've tried and tried to understand lambda calculus but my dumb ass struggles to retain the information and I just don't get it. Maybe the context-free grammar thing is too confusing for me.
So, could someone please share a good tutorial for absolute fagstorms?
multithreaded haskell factorial for the win: 2 million factorial in less than 19 seconds.
$ ./factorial 20000000 +RTS -s
37768210576(~11MB of digits snipped)0000000000
1,648,364,640 bytes allocated in the heap
325,424,052 bytes copied during GC
18,401,832 bytes maximum residency (29 sample(s))
1,815,432 bytes maximum slop
130 MB total memory in use (13 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1411 colls, 1410 par 12.93s 2.91s 0.0021s 0.0557s
Gen 1 29 colls, 29 par 3.18s 0.54s 0.0185s 0.1733s
Parallel GC work balance: 1.37 (81339519 / 59252792, ideal 8)
INIT time 0.00s ( 0.00s elapsed)
MUT time 25.47s ( 15.49s elapsed)
GC time 16.11s ( 3.44s elapsed)
EXIT time 0.00s ( 0.00s elapsed)
Total time 41.59s ( 18.93s elapsed)
Alloc rate 64,705,280 bytes per MUT second
Productivity 61.3% of total user, 134.6% of total elapsed
>>105
The entire thread is full of people described by Yossi here: http://yosefk.com/c++fqa/ctors.html#fqa-10.9 -- the people who think that they care about performance, but all they really care about is stroking their egos by participating in discussions, in this case on an anonymous textboard for added patheticalness.
#include<stdio.h>
int x[3][9999]={{1}},i=0,j,k,a,b;main(){while(i++^1001){for(x[2][0]++,j=0;x[2][j]>9;j++)x[2][j]%=10,x[2][++j]++;a=i&1,b=a^1;for(j=0;j<2568;j++)for(k=0;k<2568;k++)x[a][j+k]=x[b][j]*x[2][k]+(j?x[a][j+k]:0);for(j=0;j<2568;)x[a][j+1]+=x[a][j]/10,x[a][j++]%=10;}for(j=2567;j>=0;j--)printf("%d%s",x[i&1][j],j?"":"\n");}
$ gcc fact.c -O4
$ time ./a.out
real 0m7.641s
user 0m7.612s
sys 0m0.004s
>>112 jerking off to hentai
I never got the appeal of hentai. Ecchi, on the other hand, makes me cum massive buckets.
Maybe I am too innocent for actual hentai. ;_;
Name:
Anonymous2012-07-17 15:49
>>113
What is the sorcery with the "odd part of the swinging factorial"?
Name:
Anonymous2012-07-17 19:28
>>116
Premature "optimization" that, in Haskell at least, actually makes it a bit slower.
>>113
It still takes over a second to do what should take less than 10 milliseconds.
>>115
I understand. Hentai is nice when it's vanilla, though.
The good thing of ecchi is that it lets your imagination do whatever the hell it wants and I think it's hotter that way. While you're restricted to PLEASE INSERT YOUR OCHINCHIN INSIDE MY PUSSY KYAAAAAAAAAAA with hentai.
>>120
As >>123 pointed out, the problem isn't that it's slow. The problem is that it wastes processor time that could be used for other tasks, or energy that could be saved by not running the processor (or running it at a slower clock speed while idle).
Name:
Anonymous2012-07-18 23:59
>>123 >>124
then work a bit more and earn those extra pennies in less than 15 seconds
you're not going to die for working 5 seconds more
and stop using x86
Name:
Anonymous2012-07-19 0:12
>>125
The Lemote 8133 is not yet out, I will keep using x86.
>>125
I bet you're one of those assholes who say that 5 miles per gallon ought to be good enough for anybody. Wasting electricity means wasting fossil fuels, and no amount of work will appreciably change the amount of fossil fuels available on Earth.
Name:
Anonymous2012-07-19 3:14
>>127
My entire household is powered by wind and sun power. Checkmate, faggot.
Who Gives a fuck about faktorial i want GAMMA FUNCKTION IMPLENMENTANTION RITE NAO!!!!!!!!!!!!!
IVE BEEN TRYING FOR MONTSH TO FIGURE OUT HOW GAMMA FUNCTIONdsagdBAUIDHBADSikhjui!? AND MY NEURAL NETS DONT CONVERGE FUCK THIS SHIT
OH WAIT MY COMPILER HA S BULILT IN LIBRARY TO DO LOG_GAMMA BUT I HAVE no CLUE HOW the HELL IT DOES IT SO EXPLAIN ME THE STEPS IN GETTING THE GAMMA FUNCTION OR MAYBE POSSIBLEY EVEN AN ASM CODE SNIPPET SOR SOMETHING INSANELY CRazy LIKE TGAt LOL!!
IF YOU DONT GOING TO DO PUT CODE HERE I WILL ANALYZE THE CODE MYSELF!!!!!!!!!!!!!!!!!!!!!!! BUT THATS HRARD TO DO!!!!!!!! THEREFORE U MUST POST HERE CODE OR I WIL BE BUTTTTTTT ANGREY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!~~~
Name:
Anonymous2012-07-20 17:59
product([], 1).
product([H|T], X) :- product(T, Y), X is H * Y.
factorial(N, R) :- numlist(2, N, Ns), product(Ns, R).
Name:
Anonymous2012-07-20 22:55
>>142
This is unnecessarily memory inefficient, not tail-recursive, only works one way and it also does not conform to ISO-Prolog.
successor(X, Y) :-
number(Y) -> X is dec(Y); number(X) -> Y is inc(X);
throw(error(instantiation_error, successor/2)).
nonnegative(0).
nonnegative(N) :-
number(N) -> N > 0;
nonnegative(M), N is M + 1.
factorial(0, R, R) :- !.
factorial(N, P, R) :-
successor(M, N), A is N * R, factorial(M, P, A).
factorial(N, R) :-
nonnegative(N), factorial(N, R, 1).
Whether the query
?- factorial(100, N).
actually yields the correct number also depends on the implementations limitations when it comes to integers.
Name:
Anonymous2012-07-21 1:22
>>143
WTF does that even do? I know it squares a number, but how?
Name:
Anonymous2012-07-21 1:34
>>144
It does in fact not square any number unless you are referring to the number 1, but that is indeed the only number it will square.
As for how it is done, that is just a silly implementation detail.
>>146
Are you referring to the squaring of the number 1? Then no, it is a consequence of both 0! and 1! being 1.
Name:
Anonymous2012-07-21 1:41
2976579765 in decimal hurr
Name:
Anonymous2012-07-21 2:21
>>143
If you're going to bitch about inefficiency, you really should do it right.
product([], 1).
product([H|T], X) :- product(T, Y), X is H * Y.
sieve(N, [2|PS]) :- % PS is list of odd primes up to N
retractall(mult(_)),
sieve_O(3,N,PS).
sieve_O(I,N,PS) :- % sieve odds from I up to N to get PS
I =< N, !, I1 is I+2,
( mult(I) -> sieve_O(I1,N,PS)
; ( I =< N / I ->
ISq is I*I, DI is 2*I, add_mults(DI,ISq,N)
; true ),
PS = [I|T],
sieve_O(I1,N,T)
).
sieve_O(I,N,[]) :- I > N.
add_mults(DI,I,N) :-
I =< N, !,
( mult(I) -> true ; assert(mult(I)) ),
I1 is I+DI,
add_mults(DI,I1,N).
add_mults(_,I,N) :- I > N.
prime_power_of_s(_, 0, A, S) :- S is A.
prime_power_of_s(P, N, A, S) :- Q is div(N, P), R is mod(N, P), B is A + R, prime_power_of_s(P, Q, B, S).
prime_power_of(N, P, R) :- prime_power_of_s(P, N, 0, S), R is div(N - S, P - 1).
>>149
the fugliness of your code is only matched by its author's.
Name:
Anonymous2012-07-21 3:02
>>150
Your shitty (GNU?) implementation is showing. >>149 is tail recursive, much faster than >>143, and uses much less memory. Also, on a decent implementation, >>142 is only slower than >>143 by a small constant factor, and uses a about the same amount of memory.
Name:
Anonymous2012-07-21 3:05
>>152
And yet it doesn't even manage to answer the simple query
>>157
Yes evaluated arithmetic doesn't work both ways, so you have to actually think.
Name:
Anonymous2012-07-21 18:35
>>158
I'm pretty sure that was >>157's point, in response to >>156's irrational belief that Prolog should make thinking on his part unnecessary.
Name:
Anonymous2012-07-21 19:33
>>159
Nope that's not what I said. I said that you should design rules that work both ways, since it's Prolog and end users kind of expect usability.
There is a golden middle road here, where ISO-Prolog implementations can easily figure out what you're trying to do while also making the solution efficient, in any case the most stable and usable implementation never looks like >>142 or >>149.
Prolog was made in France, and France is fucking shit.
Go fuck an autistic ``le parfum d' snail".
Name:
Anonymous2012-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:
Anonymous2012-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.
too much of an academic masturbation even for Prolog
I seriously doubt such a thing is possible.
Name:
Anonymous2012-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.