![]() |
|
|
#1816 |
|
Aug 2006
3·1,993 Posts |
As for getting a correct answer, I'd rather write my own than correct yours:
Code:
longmultiply(a,b)= {
a=eval(Vec(Str(a)));
b=eval(Vec(Str(b)));
my(res=vector(#a+#b),carry=0);
for(i=1,#a,
for(j=1,#b,
res[i+j] += a[i]*b[j]
)
);
forstep(i=#res,2,-1,
res[i] += carry;
carry = res[i]\10;
res[i] -= 10 * carry
);
res[1] += carry;
if(res[1],
res
,
vecextract(res,2^#res-2)
)
};
|
|
|
|
|
|
#1817 |
|
"Forget I exist"
Jul 2009
Dumbassville
20C016 Posts |
wrong again that caused more errors to fix. Now I see a logic flaw on my part and using n+m as the length of answer.
|
|
|
|
|
|
#1818 | |
|
Aug 2006
10111010110112 Posts |
Quote:
The length is good. Look at my answer (post #1816). Last fiddled with by CRGreathouse on 2010-11-30 at 01:58 |
|
|
|
|
|
|
#1819 | |
|
"Forget I exist"
Jul 2009
Dumbassville
100000110000002 Posts |
Quote:
Code:
[0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] |
|
|
|
|
|
|
#1820 |
|
Aug 2006
3·1,993 Posts |
Why would you print the result of the global variable answer?
1. My function already returns a result; no need to modify it to see the answer. 2. My function doesn't use "answer" in any way, so it's no surprise that it still has the last value it had when your function used it. 3. If your function had encapsulated its variables with local() or my() then you wouldn't have changed the global "answer" in the first place so you wouldn't get this strange result. This is one of the benefits of using local() and my()! Remember: Amateurs print, professionals return. Try something like this: Code:
for(a=1,1e3,for(b=1,1e3,if(longmultiply(a,b)!=eval(Vec(Str(a*b))),print(a"*"b)))) Code:
for(a=1,1e3,for(b=1,1e3,if(longmultiply(a,b)!=eval(Vec(Str(a*b))),return([a,b])))) Last fiddled with by CRGreathouse on 2010-11-30 at 02:05 |
|
|
|
|
|
#1821 | |
|
"Forget I exist"
Jul 2009
Dumbassville
26·131 Posts |
Quote:
|
|
|
|
|
|
|
#1822 | |
|
"Forget I exist"
Jul 2009
Dumbassville
26·131 Posts |
Quote:
|
|
|
|
|
|
|
#1823 | |
|
Aug 2006
597910 Posts |
Quote:
If you did Code:
longmultiply(a,b)={
\\ blah blah blah...
print(answer)
};
If you did Code:
times10(n)={
a = 5;
b = times2(n);
a*b
};
times2(n)={
a = 2;
a * n
};
times10(1)
Code:
times10(n)={
my(a = 5, b = times2(n));
a*b
};
times2(n)={
my(a = 2);
a * n
};
times10(1)
|
|
|
|
|
|
|
#1824 |
|
"Forget I exist"
Jul 2009
Dumbassville
838410 Posts |
I couldn't get the digits placed properly as i didn't have a formula I found one that worked except a shift I made a shift of all but answer[1] then I shifted answer[1] separately, and yes you would as it determines what stays in position 1.
|
|
|
|
|
|
#1825 |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
|
|
|
|
|
|
#1826 |
|
Aug 2006
3·1,993 Posts |
So let's step back and think about that. You can't call times2() from a function unless you check it to make sure that none of its variables are the same as the ones in the calling function. But what if times2() called another function?
Code:
times2(n)={
a = minus1(n);
if(a, times2(a) + 2, 2)
};
minus1(n)={
n - 1
};
Code:
times10(n)={
b = times2(n);
theVariableFormerlyKnownAsA = 5;
theVariableFormerlyKnownAsA*b
};
Code:
minus1(n)={
b = n;
b--
}
This becomes surprisingly common with complex functions calling each other, with how few major variable names we use. But the whole issue can be solved in one step -- no looking up functions, and functions in functions, and functions in functions in functions, ... -- by using my(). |
|
|
|
![]() |
| Thread Tools | |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Why do I sometimes see all the <> formatting commands when I quote or edit? | cheesehead | Forum Feedback | 3 | 2013-05-25 12:56 |
| Passing commands to PARI on Windows | James Heinrich | Software | 2 | 2012-05-13 19:19 |
| Ubiquity commands | Mini-Geek | Aliquot Sequences | 1 | 2009-09-22 19:33 |
| 64-bit Pari? | CRGreathouse | Software | 2 | 2009-03-13 04:22 |
| Are these commands correct? | jasong | Linux | 2 | 2007-10-18 23:40 |