 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
System Monitor
Joined: 09 Mar 2007 Posts: 509 Location: Unknown
|
Posted: Sat Jul 04, 2009 2:18 am Post subject: Using Long Integer Arithmetic Library |
|
|
I can't seem to figure out how to use lazlo's long integer arithmetic library to find 100 factorial
http://www.autohotkey.com/forum/viewtopic.php?p=138409#138409
| Code: | #include mpl.ahk
MP_Set(total,1)
Loop 100
{
MP_MUL(z,total,a_index)
MP_Set(total,z)
}
total .= MP_Dec(total)
msgbox %total% |
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Sat Jul 04, 2009 2:33 am Post subject: |
|
|
You could have bumped the relevant topic. Send him a private message:  |
|
| Back to top |
|
 |
animeaime
Joined: 04 Nov 2008 Posts: 1045
|
Posted: Sat Jul 04, 2009 5:04 am Post subject: |
|
|
Check out the example function of factorial in my post. _________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts. |
|
| Back to top |
|
 |
System Monitor
Joined: 09 Mar 2007 Posts: 509 Location: Unknown
|
Posted: Sat Jul 04, 2009 7:43 am Post subject: |
|
|
thanks!
I created another function that is supposed to add up the digits of the factorial but it returns no result and i can not figure out why
| Code: | #include mpl.ahk
#SingleInstance Force
#noenv
MP_Init()
MP_set(total,0)
result := factoral(100)
len := StrLen(result)
s := digitAdd(len,result)
MsgBox, %s%
digitAdd(len,result)
{
MP_Set(total,0)
Loop %len%
{
MP_set(y,SubStr(result,A_Index,1))
MP_ADD(z,total,y)
MP_cpy(total,z)
}
return MP_Dec(total)
}
factoral(n)
{
MP_set(result, 1)
Loop, %n%
{
MP_set(tmpVal, A_Index)
MP_mul(tmpResult, result, tmpVal)
MP_cpy(result, tmpResult)
}
return MP_dec(result)
} |
|
|
| Back to top |
|
 |
animeaime
Joined: 04 Nov 2008 Posts: 1045
|
Posted: Sat Jul 04, 2009 8:09 am Post subject: |
|
|
No, your code is right on...as proof, change "len" to "len2" in your code. The problem is that "len" is a variable used in the library - check the source code. So, when you set "len" in your code, it changes the value. Then, when the library tries to use the value "len" to call a function, address, or whatever that hex value is, it calls it with the new value you set - creating A BIG MESS.
So, you'll have to be mindful of the variables used in the library and don't set them. Apart from that, the function works great, good job.
| Code: | #SingleInstance Force
#noenv
MP_Init()
MP_set(total,0)
result := factoral(100)
len2 := StrLen(result)
s := digitAdd(len2,result)
MsgBox, %s%
digitAdd(len,result)
{
MP_Set(total,0)
Loop %len%
{
MP_set(y,SubStr(result,A_Index,1))
MP_ADD(z,total,y)
MP_cpy(total,z)
}
return MP_Dec(total)
}
factoral(n)
{
MP_set(result, 1)
Loop, %n%
{
MP_set(tmpVal, A_Index)
MP_mul(tmpResult, result, tmpVal)
MP_cpy(result, tmpResult)
}
return MP_dec(result)
} |
_________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts. |
|
| Back to top |
|
 |
System Monitor
Joined: 09 Mar 2007 Posts: 509 Location: Unknown
|
Posted: Sat Jul 04, 2009 8:24 am Post subject: |
|
|
Thank you!
I must be cursed with this.
This function for generating the nth Fibonacci number doesn't work either!
| Code: | fib(nth) ;Determine the nth fibonacci number
{
MP_Init()
MP_Set(xnum,1)
MP_Set(xp,1)
MP_Set(number,nth)
if (nth = 1) or (nth = 2)
return 1
Loop % nth - 2
{
MP_ADD(znum,xnum,xp)
MP_Set(xp,xnum)
MP_Set(xnum,znum)
}
return MP_dec(znum)
} |
|
|
| Back to top |
|
 |
animeaime
Joined: 04 Nov 2008 Posts: 1045
|
Posted: Sat Jul 04, 2009 8:31 am Post subject: |
|
|
No, this one's your fault . You used MP_set to copy a value - you need to use MP_cpy. BTW, I reorder the code a bit.
| Code: | MP_Init()
MsgBox, % fib(5)
fib(nth) ;Determine the nth fibonacci number
{
if (nth = 1) or (nth = 2)
return 1
MP_Set(xnum,1)
MP_Set(xp,1)
Loop % nth - 2
{
MP_ADD(znum,xnum,xp)
MP_cpy(xp,xnum)
MP_cpy(xnum,znum)
}
return MP_dec(znum)
} |
_________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|