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 18:33

[QUOTE=CRGreathouse;239167]Ah. You should only use one, but pre-set it to the appropriate size, filled with 0s, then *add* rather than concatenate. That way you can go from

[0, 0, 0, 0, 0]
to
[0, 0, 0, 0, 6]
to
[0, 0, 0, 4, 6]
to
[0, 0, 0, 13, 6]
to
[0, 0, 6, 13, 6]
which can be simplified to
[0, 0, 7, 3, 6]
and then
[7, 3, 6]
if desired.[/QUOTE]

I was thinking of using while loops and another vector, but your way works I think. The problem is if I get 9*9 then it makes it can make it even longer still.

CRGreathouse 2010-11-29 18:39

[QUOTE=science_man_88;239206]I was thinking of using while loops and another vector, but your way works I think. The problem is if I get 9*9 then it makes it can make it even longer still.[/QUOTE]

If you multiply an n digit number by an m digit number, n + m digits will suffice (though the most significant digit may be 0).

science_man_88 2010-11-29 18:54

[QUOTE=CRGreathouse;239208]If you multiply an n digit number by an m digit number, n + m digits will suffice (though the most significant digit may be 0).[/QUOTE]

Okay I think I get the preset stuff, if I just add I get 4+9+6+6 if I have it as is.

science_man_88 2010-11-29 19:27

[CODE]answer[#answer-(x*y-1)]=(answer[#answer-(x*y-1)]+n);carry=floor((c[y]*d[x])/10)[/CODE]

getting closer I got to:

[CODE][6, 0, 13, 6][/CODE]

I know why it's happened, going to have to work out how to fix it. and it all has to do with: (x*y-1) because:
-(1*1-1) gives -0,
-(2*1-1) and -(1*2-1) give -1,But
-(2*2-1) gives -(3) so it skips -2 the place it should go in.

CRGreathouse 2010-11-29 19:38

I wouldn't worry about carries; just fix them in the end.

science_man_88 2010-11-29 19:44

[QUOTE=CRGreathouse;239221]I wouldn't worry about carries; just fix them in the end.[/QUOTE]

It's not a carry problem it's a placement formula error problem because I should of had a formula to place the 6 in the 2nd from right not the very right.

CRGreathouse 2010-11-29 20:00

[QUOTE=science_man_88;239223]It's not a carry problem it's a placement formula error problem because I should of had a formula to place the 6 in the 2nd from right not the very right.[/QUOTE]

Every time you move one digit to the left on the multiplier you need to move the result one digit to the left. :smile:

science_man_88 2010-11-29 20:04

[QUOTE=CRGreathouse;239229]Every time you move one digit to the left on the multiplier you need to move the result one digit to the left. :smile:[/QUOTE]

Instead of multiplying I should be adding lol and subtracting 1 I think. I'll try it.

science_man_88 2010-11-29 20:16

Got it working for 2 digits I'll check for 3. If it works I'll just need to chop off a 0 on the end.

science_man_88 2010-11-29 20:18

[QUOTE=science_man_88;239238]Got it working for 2 digits I'll check for 3. If it works I'll just need to chop off a 0 on the end.[/QUOTE]

oh and then the carry has to be done in the additions.

science_man_88 2010-11-29 20:52

[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)[/CODE]

One thing I can't seem to do is shift it maybe I'll have to add it in as another loop, this code is making me loopy.


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

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