### Thread: String to Int conversion speed

Page 3 of 3 First 123
1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Location
Texas
Posts
24
Rep Power
0
Originally Posted by b49P23TIvg
The division part of your program might be faster using divmod. Then again, you might end up storing a bunch of really long unused quotients. Don't know.
Code:
```>>> help(divmod)
Help on built-in function divmod in module __builtin__:

divmod(...)
divmod(x, y) -> (quotient, remainder)

Return the tuple ((x-x%y)/y, x%y).  Invariant: div*y + mod == x.```
Thanks for the divmod tip, but in this case it won't help, as I don't want to i/p unless p is a factor of i, in which case there won't be a remainder.
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Location
Texas
Posts
24
Rep Power
0
Update:

Found a faster way of doing it.

Instead of creating strings and converting them to integers, I multiply the last integer to get the new:
Code:
```
oldone=i
bigone=10**(10**(n-1))

for cnt in range (1,10):
i*=bigone
i+=oldone
```
Results:

Using the i=str('1'**n) solution takes about 12 minutes for 1 <= n <= 7:
Code:
```
07:53:01:65 - Start
08:05:00.80 - Finish
```
New solution, taking the previous i and multiplying it with 10**(10**(n-1)) and adding i back 9 times is a lot faster, taking only about 3 minutes and 5 seconds:
Code:
```
08:39:47:89 - Start
08:42:52.49 - Finish
```
Modifying the new solution to div i with the prime factor found, as well as searching the primes reversed (high to low) is about the same, about 1 second faster than the previous:
Code:
```
08:45:44.06 - Start
08:48:49.00 - Finish
```
Page 3 of 3 First 123