Hi All -

Some years ago I was taken up with solving the Project Euler problems (http://projecteuler.net/problems) and I wrote some routines for big numbers in VBA. I wondered how Applescript compared to VBA, so I translated a few. These routines work by adding digits by strings, vice as numbers.

```
property Buffer : {}
on pad(N, X)
set padding to {}
repeat with i from 1 to N
set the end of padding to X
end repeat
return padding as text
end pad
on AddByStrings(Adder1, Adder2)
set Buffer to {}
set carry to 0
set soln to 0
set L1 to length of Adder1
set L2 to length of Adder2
if L1 > L2 then
set Adder2 to pad(L1 - L2, "0") & Adder2
else if L2 > L1 then
set Adder1 to pad(L2 - L1, "0") & Adder1
end if
set L1 to length of Adder1 -- Also the length of Adder2
repeat with i from L1 to 1 by -1
set soln to carry + (text i of Adder1)
set soln to soln + (text i of Adder2)
set carry to (soln div 10)
if i â‰ 1 then
set the end of Buffer to (soln mod 10)
else
set the end of Buffer to soln
end if
end repeat
set Answer to the reverse of Buffer
return Answer as text
end AddByStrings
on MultByStrings(Multiplicand, Multiplier)
set Rslt to Multiplicand
repeat with i from 1 to Multiplier - 1
set Rslt to AddByStrings(Rslt, Multiplicand)
end repeat
return Rslt
end MultByStrings
on PowerByStrings(Nmbr, Power)
set Rslt to "1"
repeat with i from 1 to Power
set Rslt to MultByStrings(Rslt, Nmbr)
end repeat
return Rslt
end PowerByStrings
on FactorialByStrings(Nmbr)
set Rslt to "1"
repeat with i from 1 to Nmbr
set Rslt to MultByStrings(Rslt, i)
end repeat
return Rslt
end FactorialByStrings
```

Using the above, 45^45 is 248063644451341145494649182395412689744530581492654164321720600128173828125.

```
return PowerByStrings(45, 45)
```

80! is 71569457046263802294811533723186532165584657342365752577109445058227039255480148842668944867280814080000000000000000000

```
return FactorialByStrings(80)
```

1234567890*5321 is 5334567852690

```
MultByStrings("1234567890", "4321")
```

69011111111111111111111+1234521 is 69011111111111112345632

```
return AddByStrings("69011111111111111111111", "1234521")
```

Project Euler Question 16 asks: What is the sum of the digits of the number 2^1000?

You can do it at least three ways with the above.

```
set Answer to 0
set M2 to PowerByStrings(2, 1000)
repeat with i from 1 to length of M2
set Answer to Answer + (text i of M2)
end repeat
return Answer
```

or

```
set expt to 1000
set M1 to "2"
set Answer to 0
repeat with i from 2 to expt
set M2 to AddByStrings(M1, M1)
set M1 to M2
end repeat
repeat with i from 1 to length of M2
set Answer to Answer + (text i of M2)
end repeat
return Answer
```

or

```
set M2 to "2"
set Answer to 0
repeat with i from 2 to 1000
set M2 to MultByStrings(M2, 2)
end repeat
repeat with i from 1 to length of M2
set Answer to Answer + (text i of M2)
end repeat
return Answer
```

All versions take about 9 seconds. Not to give away the answer, but 2^1000 is 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 exactly.

Applescript suffers speedwise vs. VBA. I’d be interested in any tips to speed this up.

…Mick