mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   PARI/GP (https://www.mersenneforum.org/forumdisplay.php?f=155)
-   -   PARI's commands (https://www.mersenneforum.org/showthread.php?t=13636)

science_man_88 2010-11-29 21:17

[CODE]longmultiply(a,b)= c=eval(Vec(Str(a)));d=eval(Vec(Str(b)));carry=0;answer=vector(length(d)+length(c));forstep(x=#d,1,[-1],forstepn=((c[y]*d[x])+carry)%10;answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);carry=floor((c[y]*d[x])/10)));forstep(w=#answer,1,-1,if(w>1,answer[w-1]=answer[w-1]+floor(answer[w]/10));answer[w]=answer[w]%10;if(w<#answer,answer[w+1]=answer[w]))[/CODE] There we are lol, only 398 characters long! Anyone want to better this ? sorry even more for a good copy apparently lol, well I've lost a part I'll be back.

science_man_88 2010-11-29 22:04

[CODE]longmultiply(a,b)= c=eval(Vec(Str(a)));d=eval(Vec(Str(b)));carry=0;answer=vector(length(d)+length(c));forstep(x=#d,1,[-1],forstep(y=#c,1,[-1],n=((c[y]*d[x])+carry)%10;answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);carry=floor((c[y]*d[x])/10)));forstep(w=#answer,1,-1,if(w>1,answer[w-1]=answer[w-1]+floor(answer[w]/10));answer[w]=answer[w]%10;if(w<#answer,answer[w+1]=answer[w]))[/CODE]

Found my best answer in Pari still only works if the answer is n+m digits. 399 characters. not sure how to compensate for the last part credit should go to CRG in fact I have to give him credit for the others as well I'll correct that.

science_man_88 2010-11-29 22:37

add[CODE];answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer[/CODE] onto it I think it then works.

science_man_88 2010-11-29 22:39

[QUOTE=science_man_88;239257]add[CODE];answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer[/CODE] onto it I think it then works.[/QUOTE]

[CODE]longmultiply(a,b)= [COLOR="Red"]c=eval(Vec(Str(a)));d=eval(Vec(Str(b)))[/COLOR];carry=0;[COLOR="red"]answer=vector(length(d)+length(c))[/COLOR];forstep(x=#d,1,[-1],forstep(y=#c,1,[-1],n=((c[y]*d[x])+carry)%10;answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);carry=floor((c[y]*d[x])/10)));forstep(w=#answer,1,-1,if(w>1,answer[w-1]=answer[w-1]+floor(answer[w]/10));answer[w]=answer[w]%10;if(w<#answer,answer[w+1]=answer[w]));answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer[/CODE] I believe. Now it's 471 characters, That's a long code in my books lol.

found a flaw in my add on.

without the addon lower ones work with it low ones don't. But for higher ones it messes it up the other way round I think.

4now I've got 33*33 not working man I suck at this.

science_man_88 2010-11-29 23:19

[CODE]longmultiply(a,b)= c=eval(Vec(Str(a)));d=eval(Vec(Str(b)));carry=0;answer=vector(length(d)+length(c));forstep(x=#d,1,[-1],forstep(y=#c,1,[-1],n=((c[y]*d[x])+carry)%10;answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);carry=floor((c[y]*d[x])/10)));forstep(w=#answer,2,-1,answer[w-1]=answer[w-1]+floor(answer[w]/10);answer[w]=answer[w]%10;if(w<#answer,answer[w+1]=answer[w]));answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer[/CODE]

I'm quiting this for now if this isn't it lol. It took me multiple post on here and 255 + answers in Pari.

science_man_88 2010-11-29 23:41

[CODE]*** bug in PARI/GP (Segmentation Fault), please report[/CODE]

not good I may close Pari lol just to restart.

science_man_88 2010-11-30 00:08

[QUOTE=science_man_88;239269][CODE]longmultiply(a,b)= c=eval(Vec(Str(a)));d=eval(Vec(Str(b)));carry=0;answer=vector(length(d)+length(c));forstep(x=#d,1,[-1],forstep(y=#c,1,[-1],n=((c[y]*d[x])+carry)%10;answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);carry=floor((c[y]*d[x])/10)));forstep(w=#answer,2,-1,answer[w-1]=answer[w-1]+floor(answer[w]/10);answer[w]=answer[w]%10;if(w<#answer,answer[w+1]=answer[w]));answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer[/CODE]

I'm quiting this for now if this isn't it lol. It took me multiple post on here and 255 + answers in Pari.[/QUOTE]

and adding:

[CODE]if(answer==eval(Vec(Str(a*b))),print(answer))[/CODE] I get:

[CODE][1, 0, 0, 1]
[1, 0, 1, 2]
[1, 0, 2, 3]
[1, 0, 3, 4]
[1, 0, 4, 5]
[1, 0, 5, 6]
[1, 0, 6, 7]
[1, 0, 7, 8]
[1, 0, 8, 9]
[1, 0, 0, 8]
[1, 0, 2, 0]
[1, 0, 3, 2]
[1, 0, 4, 4]
[1, 0, 5, 6]
[1, 0, 0, 1]
[1, 0, 0, 8]
[1, 0, 2, 2]
[1, 0, 3, 6]
[1, 0, 5, 0]
[1, 0, 6, 4]
[1, 0, 7, 8]
[1, 0, 0, 8]
[1, 0, 2, 4]
[1, 0, 4, 0]
[1, 0, 5, 6]
[1, 0, 0, 7]
[1, 0, 2, 6]
[1, 0, 4, 5]
[1, 0, 1, 2]
[1, 0, 0, 8]
[1, 0, 3, 2]
[1, 0, 5, 6]
[1, 0, 2, 3]
[1, 0, 2, 4]
[1, 0, 5, 6]
[1, 0, 2, 3]
[1, 0, 5, 6]
[1, 0, 8, 9]
[1, 0, 1, 2]
[1, 0, 3, 4]
[1, 0, 0, 8]
[1, 0, 5, 6]
[1, 0, 2, 9]
[1, 0, 7, 8]
[1, 0, 0, 1]
[1, 0, 1, 2]
[1, 0, 2, 3]
[1, 0, 3, 4]
[1, 0, 4, 5]
[1, 0, 5, 6]
[1, 0, 6, 7]
[1, 0, 7, 8]
[1, 0, 8, 9][/CODE]

far from what is expected for:

[CODE]for(x=1,100,for(y=1,100,longmultiply(x,y)))[/CODE]

CRGreathouse 2010-11-30 01:22

First of all, your code seems to be nearly there -- it works in many cases. Some of the cases where it doesn't work are as simple as having an initial 0 -- in those cases you can just replace the final
[code];answer[/code]
with
[code];if(answer[1],answer,vecextract(answer,2^#answer-2))[/code]
to drop the zero. (I suppose you should also test to see if one number is 0 since in that case you'll only need one digit.)

I'm not sure what the problem is in the other case; I've avoided looking at your code since it's not formatted. But keep looking at the cases that it fails at -- why those and not others? What's going wrong there?

science_man_88 2010-11-30 01:27

[QUOTE=CRGreathouse;239288]First of all, your code seems to be nearly there -- it works in many cases. Some of the cases where it doesn't work are as simple as having an initial 0 -- in those cases you can just replace the final
[code];answer[/code]
with
[code];if(answer[1],answer,vecextract(answer,2^#answer-2))[/code]
to drop the zero. (I suppose you should also test to see if one number is 0 since in that case you'll only need one digit.)

I'm not sure what the problem is in the other case; I've avoided looking at your code since it's not formatted. But keep looking at the cases that it fails at -- why those and not others? What's going wrong there?[/QUOTE]

[CODE]longmultiply(a,b)= {
[COLOR="Red"]c=eval(Vec(Str(a)));
d=eval(Vec(Str(b)));[/COLOR]
carry=0;
[COLOR="red"]answer=vector(length(d)+length(c));[/COLOR]
forstep(x=#d,1,[-1],
forstep(y=#c,1,[-1],
n=((c[y]*d[x])+carry)%10;
answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);
carry=floor((c[y]*d[x])/10)));
forstep(w=#answer,2,-1,
answer[w-1]=answer[w-1]+floor(answer[w]/10);
answer[w]=answer[w]%10;
if(w<#answer,answer[w+1]=answer[w]));
answer[2]=answer[1]%10;
answer[1]=floor(answer[1]/10);
answer
}[/CODE]

is this formatted better ?

CRGreathouse 2010-11-30 01:43

[QUOTE=science_man_88;239292]is this formatted better ?[/QUOTE]

A little. Here's how I'd format it:
[code]longmultiply(a,b)= {
my(c=eval(Vec(Str(a))),d=eval(Vec(Str(b))),carry=0,answer=vector(#c+#d));
forstep(x=#d,1,[-1],
forstep(y=#c,1,[-1],
n=((c[y]*d[x])+carry)%10;
answer[#answer-((#d-x+1)+(#c-y+1)-1)] += n;
carry=(c[y]*d[x])\10
)
);
forstep(w=#answer,2,-1,
answer[w-1] += answer[w]\10;
answer[w]=answer[w]%10;
if(w<#answer,answer[w+1]=answer[w])
);
answer[2]=answer[1]%10;
answer[1]=answer[1]\10;
answer
};[/code]

Also changed: length(foo) to #foo, floor(foo/10) to foo\10, foo = foo + n to foo += n, local variables put into a my block. (This is important if you want to avoid clobbering variables! Always do this!)

science_man_88 2010-11-30 01:49

[QUOTE=CRGreathouse;239295]A little. Here's how I'd format it:
[code]longmultiply(a,b)= {
my(c=eval(Vec(Str(a))),d=eval(Vec(Str(b))),carry=0,answer=vector(#c+#d));
forstep(x=#d,1,[-1],
forstep(y=#c,1,[-1],
n=((c[y]*d[x])+carry)%10;
answer[#answer-((#d-x+1)+(#c-y+1)-1)] += n;
carry=(c[y]*d[x])\10
)
);
forstep(w=#answer,2,-1,
answer[w-1] += answer[w]\10;
answer[w]=answer[w]%10;
if(w<#answer,answer[w+1]=answer[w])
);
answer[2]=answer[1]%10;
[COLOR="Red"]answer[1]=answer[1]\10;[/COLOR]
answer
};[/code]

Also changed: length(foo) to #foo, floor(foo/10) to foo\10, foo = foo + n to foo += n, local variables put into a my block. (This is important if you want to avoid clobbering variables! Always do this!)[/QUOTE]

the red is the starts with 0 error, I should add an if around it checking if answer[1]>10 first if so i doubt after the previous step that answer[1] is useful.


All times are UTC. The time now is 23:10.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.