-  [WT]  [PS]  [Home] [Manage]

[Return]
Posting mode: Reply
  1.   (reply to 4737)
  2. (for post and file deletion)
/pr/ - Programming
  • Supported file types are: C, CSS, DOC, DOCX, GIF, H, JAVA, JPG, PDF, PNG, SVG, SWF, TXT, WEBM
  • Maximum file size allowed is 10000 KB.
  • Images greater than 200x200 pixels will be thumbnailed.
  • Currently 389 unique user posts. View catalog

  • Blotter updated: 2011-01-12 Show/Hide Show All

There's a new /777/ up, it's /gardening/ Check it out. Suggest new /777/s here.

Movies & TV 24/7 via Channel7: Web Player, .m3u file. Music via Radio7: Web Player, .m3u file.

WebM is now available sitewide! Please check this thread for more info.

Neckbearded Basement Dweller 15/05/27(Wed)17:08 No. 4737
4737

File 143273931665.png - (12.44KB , 1025x176 , 145.png )

I'm trying to figure out this problem. Here's my code in python:

def reversible(limit):
num_list = ()
for num in range(1,limit):
num = str(num)
if num[-1] == 0:
break
else:
sum = int(num) + int(num[::-1])
checker = 0
for i in str(sum):

if int(i) % 2 == 0:
break
else:
checker += 1
if checker == len(str(sum)):
if int(num) not in num_list:
if int(num[-1]) != 0:
num_list = num_list + (num,)

checker = 0
count = 0
for x in num_list:
count += 1
return count

def main():
print(reversible(1000000))

main()

it works, but the only problem is it takes extremely long to solve. I've only managed to solve up to 20 million, and that took 10 -20 minutes. I used a tuple instead of a list, and that helped a bit but it's still slow. Is there an alternative, like more conditions to break or not use range?


>>
Neckbearded Basement Dweller 15/05/28(Thu)17:33 No. 4738

I dont think you need to keep a listing, just print them out because you should be able to predict the duplicates
I dont know the answer but for the numbers 11 thru 99 if you arrange them as a 2D matrix (ie 11..19 is the 1st row, 21..29 is the 2nd row, etc) you should see the pattern of where the duplicates are. And I believe (I am not sure) as you go thru each row you can cancel out a column in the matrix.


>>
Neckbearded Basement Dweller 15/05/28(Thu)17:38 No. 4739

Say you have a number 13739, is there an algorithm to break that down to its two components x and reverse(x)?


>>
Neckbearded Basement Dweller 15/06/15(Mon)21:45 No. 4761

>>4739
Most of the cause of the slow speed is because python loops are slow. You can look up numba, which is a jit compiler that can speed up numerical computations like this very well.

If you really want to reverse a number without converting to string, you could do mod 10, subtract result and divide 10. Numba should speed that up nicely since it's a numerical loop.


>>
Neckbearded Basement Dweller 15/07/13(Mon)20:52 No. 4768

my solution (quick n dirty)

n=0for i in xrange(10,1000000000): if i%10==0: pass else: c=False j=int(str(i)[::-1]) for k in str(i+j): if int(k)%2==0: c=True if c==False: n+=1print n


Solution still unknown coz the program is still running (on an ARM v6 lol), I expect it to finish by tomorrow morning.



[Return] [Entire Thread] [Last 50 posts]


Delete post []
Password  
Report post
Reason