Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#31
November 9th, 2012, 08:55 PM
 KING-OLE
Registered User

Join Date: Nov 2012
Location: Texas
Posts: 24
Time spent in forums: 5 h 54 m 45 sec
Reputation Power: 0
Quote:
 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.

#32
November 10th, 2012, 11:39 AM
 KING-OLE
Registered User

Join Date: Nov 2012
Location: Texas
Posts: 24
Time spent in forums: 5 h 54 m 45 sec
Reputation 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
```

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > String to Int conversion speed