Problem p03329 (python)

Fixed submission:
import math
N = int(input())
def greed(N, a):
    if a == 6:
        L = [6**i for i in range(7, -1, -1)]
    else:
        L = [9**i for i in range(6, -1, -1)]
    num = 0
    while True:
        if N == 0:
            return num
        for l in L:
            if l <= N:
                N -= l
                break
        num += 1
A = []
for n in range(N+1):
    A.append(greed(n, 6) + greed(N-n, 9))
print(min(A))

Buggy submission (verdict: Runtime Error):
import math
N = int(input())
def greed(N, a):
    num = 0
    while True:
        if N == 0:
            return num
        p = int(math.log2(N) / math.log2(a))
        N -= a**p
        num += 1
A = []
for n in range(N+1):
    A.append(greed(n, 6) + greed(N-n, 9))
print(min(A))

Buggy submission (verdict: Runtime Error):
import math
N = int(input())
def greed(N, a):
    num = 0
    while True:
        if N == 0:
            return num
        p = int(math.log2(N) / math.log2(a))
        N -= a**p
        num += 1
A = []
for n in range(N+1):
    A.append(greed(n, 6) + greed(N-n, 9))
print(min(A))

Buggy submission (verdict: Runtime Error):
dic = {1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 1,
7: 2,
8: 3,
9: 1,
10: 2,
11: 3,
12: 2,
13: 3,
14: 4,
15: 2,
16: 3,
17: 4,
18: 2,
19: 3,
20: 4,
21: 3,
22: 4,
23: 5,
24: 3,
25: 4,
26: 5,
27: 3,
28: 4,
29: 5,
30: 4,
31: 5,
32: 6,
33: 4,
34: 5,
35: 6,
36: 1,
37: 2,
38: 3,
39: 4,
40: 5,
41: 6,
42: 2,
43: 3,
44: 4,
45: 2,
46: 3,
47: 4,
48: 3,
49: 4,
50: 5,
51: 3,
52: 4,
53: 5,
54: 3,
55: 4,
56: 5,
57: 4,
58: 5,
59: 6,
60: 4,
61: 5,
62: 6,
63: 4,
64: 5,
65: 6,
66: 5,
67: 6,
68: 7,
69: 5,
70: 6,
71: 7,
72: 2,
73: 3,
74: 4,
75: 5,
76: 6,
77: 7,
78: 3,
79: 4,
80: 5,
81: 1,
82: 2,
83: 3,
84: 4,
85: 5,
86: 6,
87: 2,
88: 3,
89: 4,
90: 2,
91: 3,
92: 4,
93: 3,
94: 4,
95: 5,
96: 3,
97: 4,
98: 5,
99: 3,
100: 4,
101: 5,
102: 4,
103: 5,
104: 6,
105: 4,
106: 5,
107: 6,
108: 3,
109: 4,
110: 5,
111: 5,
112: 6,
113: 7,
114: 4,
115: 5,
116: 6,
117: 2,
118: 3,
119: 4,
120: 5,
121: 6,
122: 7,
123: 3,
124: 4,
125: 5,
126: 3,
127: 4,
128: 5,
129: 4,
130: 5,
131: 6,
132: 4,
133: 5,
134: 6,
135: 4,
136: 5,
137: 6,
138: 5,
139: 6,
140: 7,
141: 5,
142: 6,
143: 7,
144: 4,
145: 5,
146: 6,
147: 6,
148: 7,
149: 8,
150: 5,
151: 6,
152: 7,
153: 3,
154: 4,
155: 5,
156: 6,
157: 7,
158: 8,
159: 4,
160: 5,
161: 6,
162: 2,
163: 3,
164: 4,
165: 5,
166: 6,
167: 7,
168: 3,
169: 4,
170: 5,
171: 3,
172: 4,
173: 5,
174: 4,
175: 5,
176: 6,
177: 4,
178: 5,
179: 6,
180: 4,
181: 5,
182: 6,
183: 5,
184: 6,
185: 7,
186: 5,
187: 6,
188: 7,
189: 4,
190: 5,
191: 6,
192: 6,
193: 7,
194: 8,
195: 5,
196: 6,
197: 7,
198: 3,
199: 4,
200: 5,
201: 6,
202: 7,
203: 8,
204: 4,
205: 5,
206: 6,
207: 4,
208: 5,
209: 6,
210: 5,
211: 6,
212: 7,
213: 5,
214: 6,
215: 7,
216: 1,
217: 2,
218: 3,
219: 4,
220: 5,
221: 6,
222: 2,
223: 3,
224: 4,
225: 2,
226: 3,
227: 4,
228: 3,
229: 4,
230: 5,
231: 3,
232: 4,
233: 5,
234: 3,
235: 4,
236: 5,
237: 4,
238: 5,
239: 6,
240: 4,
241: 5,
242: 6,
243: 3,
244: 4,
245: 5,
246: 5,
247: 6,
248: 7,
249: 4,
250: 5,
251: 6,
252: 2,
253: 3,
254: 4,
255: 5,
256: 6,
257: 7,
258: 3,
259: 4,
260: 5,
261: 3,
262: 4,
263: 5,
264: 4,
265: 5,
266: 6,
267: 4,
268: 5,
269: 6,
270: 4,
271: 5,
272: 6,
273: 5,
274: 6,
275: 7,
276: 5,
277: 6,
278: 7,
279: 4,
280: 5,
281: 6,
282: 6,
283: 7,
284: 8,
285: 5,
286: 6,
287: 7,
288: 3,
289: 4,
290: 5,
291: 6,
292: 7,
293: 8,
294: 4,
295: 5,
296: 6,
297: 2,
298: 3,
299: 4,
300: 5,
301: 6,
302: 7,
303: 3,
304: 4,
305: 5,
306: 3,
307: 4,
308: 5,
309: 4,
310: 5,
311: 6,
312: 4,
313: 5,
314: 6,
315: 4,
316: 5,
317: 6,
318: 5,
319: 6,
320: 7,
321: 5,
322: 6,
323: 7,
324: 4,
325: 5,
326: 6,
327: 6,
328: 7,
329: 8,
330: 5,
331: 6,
332: 7,
333: 3,
334: 4,
335: 5,
336: 6,
337: 7,
338: 8,
339: 4,
340: 5,
341: 6,
342: 4,
343: 5,
344: 6,
345: 5,
346: 6,
347: 7,
348: 5,
349: 6,
350: 7,
351: 5,
352: 6,
353: 7,
354: 6,
355: 7,
356: 8,
357: 6,
358: 7,
359: 8,
360: 5,
361: 6,
362: 7,
363: 7,
364: 8,
365: 9,
366: 6,
367: 7,
368: 8,
369: 4,
370: 5,
371: 6,
372: 7,
373: 8,
374: 9,
375: 5,
376: 6,
377: 7,
378: 3,
379: 4,
380: 5,
381: 6,
382: 7,
383: 8,
384: 4,
385: 5,
386: 6,
387: 4,
388: 5,
389: 6,
390: 5,
391: 6,
392: 7,
393: 5,
394: 6,
395: 7,
396: 5,
397: 6,
398: 7,
399: 6,
400: 7,
401: 8,
402: 6,
403: 7,
404: 8,
405: 5,
406: 6,
407: 7,
408: 7,
409: 8,
410: 9,
411: 6,
412: 7,
413: 8,
414: 4,
415: 5,
416: 6,
417: 7,
418: 8,
419: 9,
420: 5,
421: 6,
422: 7,
423: 5,
424: 6,
425: 7,
426: 6,
427: 7,
428: 8,
429: 6,
430: 7,
431: 8,
432: 2,
433: 3,
434: 4,
435: 5,
436: 6,
437: 7,
438: 3,
439: 4,
440: 5,
441: 3,
442: 4,
443: 5,
444: 4,
445: 5,
446: 6,
447: 4,
448: 5,
449: 6,
450: 4,
451: 5,
452: 6,
453: 5,
454: 6,
455: 7,
456: 5,
457: 6,
458: 7,
459: 4,
460: 5,
461: 6,
462: 6,
463: 7,
464: 8,
465: 5,
466: 6,
467: 7,
468: 3,
469: 4,
470: 5,
471: 6,
472: 7,
473: 8,
474: 4,
475: 5,
476: 6,
477: 4,
478: 5,
479: 6,
480: 5,
481: 6,
482: 7,
483: 5,
484: 6,
485: 7,
486: 5,
487: 6,
488: 7,
489: 6,
490: 7,
491: 8,
492: 6,
493: 7,
494: 8,
495: 5,
496: 6,
497: 7,
498: 7,
499: 8,
500: 9,
501: 6,
502: 7,
503: 8,
504: 4,
505: 5,
506: 6,
507: 7,
508: 8,
509: 9,
510: 5,
511: 6,
512: 7,
513: 3,
514: 4,
515: 5,
516: 6,
517: 7,
518: 8,
519: 4,
520: 5,
521: 6,
522: 4,
523: 5,
524: 6,
525: 5,
526: 6,
527: 7,
528: 5,
529: 6,
530: 7,
531: 5,
532: 6,
533: 7,
534: 6,
535: 7,
536: 8,
537: 6,
538: 7,
539: 8,
540: 5,
541: 6,
542: 7,
543: 7,
544: 8,
545: 9,
546: 6,
547: 7,
548: 8,
549: 4,
550: 5,
551: 6,
552: 7,
553: 8,
554: 9,
555: 5,
556: 6,
557: 7,
558: 5,
559: 6,
560: 7,
561: 6,
562: 7,
563: 8,
564: 6,
565: 7,
566: 8,
567: 6,
568: 7,
569: 8,
570: 7,
571: 8,
572: 9,
573: 7,
574: 8,
575: 9,
576: 6,
577: 7,
578: 8,
579: 8,
580: 9,
581: 10,
582: 7,
583: 8,
584: 9,
585: 5,
586: 6,
587: 7,
588: 8,
589: 9,
590: 10,
591: 6,
592: 7,
593: 8,
594: 4,
595: 5,
596: 6,
597: 7,
598: 8,
599: 9,
600: 5,
601: 6,
602: 7,
603: 5,
604: 6,
605: 7,
606: 6,
607: 7,
608: 8,
609: 6,
610: 7,
611: 8,
612: 6,
613: 7,
614: 8,
615: 7,
616: 8,
617: 9,
618: 7,
619: 8,
620: 9,
621: 6,
622: 7,
623: 8,
624: 8,
625: 9,
626: 10,
627: 7,
628: 8,
629: 9,
630: 5,
631: 6,
632: 7,
633: 8,
634: 9,
635: 10,
636: 6,
637: 7,
638: 8,
639: 6,
640: 7,
641: 8,
642: 7,
643: 8,
644: 9,
645: 7,
646: 8,
647: 9,
648: 3,
649: 4,
650: 5,
651: 6,
652: 7,
653: 8,
654: 4,
655: 5,
656: 6,
657: 4,
658: 5,
659: 6,
660: 5,
661: 6,
662: 7,
663: 5,
664: 6,
665: 7,
666: 5,
667: 6,
668: 7,
669: 6,
670: 7,
671: 8,
672: 6,
673: 7,
674: 8,
675: 5,
676: 6,
677: 7,
678: 7,
679: 8,
680: 9,
681: 6,
682: 7,
683: 8,
684: 4,
685: 5,
686: 6,
687: 7,
688: 8,
689: 9,
690: 5,
691: 6,
692: 7,
693: 5,
694: 6,
695: 7,
696: 6,
697: 7,
698: 8,
699: 6,
700: 7,
701: 8,
702: 6,
703: 7,
704: 8,
705: 7,
706: 8,
707: 9,
708: 7,
709: 8,
710: 9,
711: 6,
712: 7,
713: 8,
714: 8,
715: 9,
716: 10,
717: 7,
718: 8,
719: 9,
720: 5,
721: 6,
722: 7,
723: 8,
724: 9,
725: 10,
726: 6,
727: 7,
728: 8}

import math
N = int(input())

def rec(N, num, A):
    if N <= 728:
        A.append(num + dic[N])
    else:
        p = int(math.log(N) / math.log(6))
        rec(N - 6**p, num+1, A)
        p = int(math.log(N) / math.log(9))
        rec(N - 9**p, num+1, A)
    return A

A = rec(N, 0, [])
print(min(A))

Buggy submission (verdict: Runtime Error):
dic = {1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 1,
7: 2,
8: 3,
9: 1,
10: 2,
11: 3,
12: 2,
13: 3,
14: 4,
15: 2,
16: 3,
17: 4,
18: 2,
19: 3,
20: 4,
21: 3,
22: 4,
23: 5,
24: 3,
25: 4,
26: 5,
27: 3,
28: 4,
29: 5,
30: 4,
31: 5,
32: 6,
33: 4,
34: 5,
35: 6,
36: 1,
37: 2,
38: 3,
39: 4,
40: 5,
41: 6,
42: 2,
43: 3,
44: 4,
45: 2,
46: 3,
47: 4,
48: 3,
49: 4,
50: 5,
51: 3,
52: 4,
53: 5,
54: 3,
55: 4,
56: 5,
57: 4,
58: 5,
59: 6,
60: 4,
61: 5,
62: 6,
63: 4,
64: 5,
65: 6,
66: 5,
67: 6,
68: 7,
69: 5,
70: 6,
71: 7,
72: 2,
73: 3,
74: 4,
75: 5,
76: 6,
77: 7,
78: 3,
79: 4,
80: 5,
81: 1,
82: 2,
83: 3,
84: 4,
85: 5,
86: 6,
87: 2,
88: 3,
89: 4,
90: 2,
91: 3,
92: 4,
93: 3,
94: 4,
95: 5,
96: 3,
97: 4,
98: 5,
99: 3,
100: 4,
101: 5,
102: 4,
103: 5,
104: 6,
105: 4,
106: 5,
107: 6,
108: 3,
109: 4,
110: 5,
111: 5,
112: 6,
113: 7,
114: 4,
115: 5,
116: 6,
117: 2,
118: 3,
119: 4,
120: 5,
121: 6,
122: 7,
123: 3,
124: 4,
125: 5,
126: 3,
127: 4,
128: 5,
129: 4,
130: 5,
131: 6,
132: 4,
133: 5,
134: 6,
135: 4,
136: 5,
137: 6,
138: 5,
139: 6,
140: 7,
141: 5,
142: 6,
143: 7,
144: 4,
145: 5,
146: 6,
147: 6,
148: 7,
149: 8,
150: 5,
151: 6,
152: 7,
153: 3,
154: 4,
155: 5,
156: 6,
157: 7,
158: 8,
159: 4,
160: 5,
161: 6,
162: 2,
163: 3,
164: 4,
165: 5,
166: 6,
167: 7,
168: 3,
169: 4,
170: 5,
171: 3,
172: 4,
173: 5,
174: 4,
175: 5,
176: 6,
177: 4,
178: 5,
179: 6,
180: 4,
181: 5,
182: 6,
183: 5,
184: 6,
185: 7,
186: 5,
187: 6,
188: 7,
189: 4,
190: 5,
191: 6,
192: 6,
193: 7,
194: 8,
195: 5,
196: 6,
197: 7,
198: 3,
199: 4,
200: 5,
201: 6,
202: 7,
203: 8,
204: 4,
205: 5,
206: 6,
207: 4,
208: 5,
209: 6,
210: 5,
211: 6,
212: 7,
213: 5,
214: 6,
215: 7,
216: 1,
217: 2,
218: 3,
219: 4,
220: 5,
221: 6,
222: 2,
223: 3,
224: 4,
225: 2,
226: 3,
227: 4,
228: 3,
229: 4,
230: 5,
231: 3,
232: 4,
233: 5,
234: 3,
235: 4,
236: 5,
237: 4,
238: 5,
239: 6,
240: 4,
241: 5,
242: 6,
243: 3,
244: 4,
245: 5,
246: 5,
247: 6,
248: 7,
249: 4,
250: 5,
251: 6,
252: 2,
253: 3,
254: 4,
255: 5,
256: 6,
257: 7,
258: 3,
259: 4,
260: 5,
261: 3,
262: 4,
263: 5,
264: 4,
265: 5,
266: 6,
267: 4,
268: 5,
269: 6,
270: 4,
271: 5,
272: 6,
273: 5,
274: 6,
275: 7,
276: 5,
277: 6,
278: 7,
279: 4,
280: 5,
281: 6,
282: 6,
283: 7,
284: 8,
285: 5,
286: 6,
287: 7,
288: 3,
289: 4,
290: 5,
291: 6,
292: 7,
293: 8,
294: 4,
295: 5,
296: 6,
297: 2,
298: 3,
299: 4,
300: 5,
301: 6,
302: 7,
303: 3,
304: 4,
305: 5,
306: 3,
307: 4,
308: 5,
309: 4,
310: 5,
311: 6,
312: 4,
313: 5,
314: 6,
315: 4,
316: 5,
317: 6,
318: 5,
319: 6,
320: 7,
321: 5,
322: 6,
323: 7,
324: 4,
325: 5,
326: 6,
327: 6,
328: 7,
329: 8,
330: 5,
331: 6,
332: 7,
333: 3,
334: 4,
335: 5,
336: 6,
337: 7,
338: 8,
339: 4,
340: 5,
341: 6,
342: 4,
343: 5,
344: 6,
345: 5,
346: 6,
347: 7,
348: 5,
349: 6,
350: 7,
351: 5,
352: 6,
353: 7,
354: 6,
355: 7,
356: 8,
357: 6,
358: 7,
359: 8,
360: 5,
361: 6,
362: 7,
363: 7,
364: 8,
365: 9,
366: 6,
367: 7,
368: 8,
369: 4,
370: 5,
371: 6,
372: 7,
373: 8,
374: 9,
375: 5,
376: 6,
377: 7,
378: 3,
379: 4,
380: 5,
381: 6,
382: 7,
383: 8,
384: 4,
385: 5,
386: 6,
387: 4,
388: 5,
389: 6,
390: 5,
391: 6,
392: 7,
393: 5,
394: 6,
395: 7,
396: 5,
397: 6,
398: 7,
399: 6,
400: 7,
401: 8,
402: 6,
403: 7,
404: 8,
405: 5,
406: 6,
407: 7,
408: 7,
409: 8,
410: 9,
411: 6,
412: 7,
413: 8,
414: 4,
415: 5,
416: 6,
417: 7,
418: 8,
419: 9,
420: 5,
421: 6,
422: 7,
423: 5,
424: 6,
425: 7,
426: 6,
427: 7,
428: 8,
429: 6,
430: 7,
431: 8,
432: 2,
433: 3,
434: 4,
435: 5,
436: 6,
437: 7,
438: 3,
439: 4,
440: 5,
441: 3,
442: 4,
443: 5,
444: 4,
445: 5,
446: 6,
447: 4,
448: 5,
449: 6,
450: 4,
451: 5,
452: 6,
453: 5,
454: 6,
455: 7,
456: 5,
457: 6,
458: 7,
459: 4,
460: 5,
461: 6,
462: 6,
463: 7,
464: 8,
465: 5,
466: 6,
467: 7,
468: 3,
469: 4,
470: 5,
471: 6,
472: 7,
473: 8,
474: 4,
475: 5,
476: 6,
477: 4,
478: 5,
479: 6,
480: 5,
481: 6,
482: 7,
483: 5,
484: 6,
485: 7,
486: 5,
487: 6,
488: 7,
489: 6,
490: 7,
491: 8,
492: 6,
493: 7,
494: 8,
495: 5,
496: 6,
497: 7,
498: 7,
499: 8,
500: 9,
501: 6,
502: 7,
503: 8,
504: 4,
505: 5,
506: 6,
507: 7,
508: 8,
509: 9,
510: 5,
511: 6,
512: 7,
513: 3,
514: 4,
515: 5,
516: 6,
517: 7,
518: 8,
519: 4,
520: 5,
521: 6,
522: 4,
523: 5,
524: 6,
525: 5,
526: 6,
527: 7,
528: 5,
529: 6,
530: 7,
531: 5,
532: 6,
533: 7,
534: 6,
535: 7,
536: 8,
537: 6,
538: 7,
539: 8,
540: 5,
541: 6,
542: 7,
543: 7,
544: 8,
545: 9,
546: 6,
547: 7,
548: 8,
549: 4,
550: 5,
551: 6,
552: 7,
553: 8,
554: 9,
555: 5,
556: 6,
557: 7,
558: 5,
559: 6,
560: 7,
561: 6,
562: 7,
563: 8,
564: 6,
565: 7,
566: 8,
567: 6,
568: 7,
569: 8,
570: 7,
571: 8,
572: 9,
573: 7,
574: 8,
575: 9,
576: 6,
577: 7,
578: 8,
579: 8,
580: 9,
581: 10,
582: 7,
583: 8,
584: 9,
585: 5,
586: 6,
587: 7,
588: 8,
589: 9,
590: 10,
591: 6,
592: 7,
593: 8,
594: 4,
595: 5,
596: 6,
597: 7,
598: 8,
599: 9,
600: 5,
601: 6,
602: 7,
603: 5,
604: 6,
605: 7,
606: 6,
607: 7,
608: 8,
609: 6,
610: 7,
611: 8,
612: 6,
613: 7,
614: 8,
615: 7,
616: 8,
617: 9,
618: 7,
619: 8,
620: 9,
621: 6,
622: 7,
623: 8,
624: 8,
625: 9,
626: 10,
627: 7,
628: 8,
629: 9,
630: 5,
631: 6,
632: 7,
633: 8,
634: 9,
635: 10,
636: 6,
637: 7,
638: 8,
639: 6,
640: 7,
641: 8,
642: 7,
643: 8,
644: 9,
645: 7,
646: 8,
647: 9,
648: 3,
649: 4,
650: 5,
651: 6,
652: 7,
653: 8,
654: 4,
655: 5,
656: 6,
657: 4,
658: 5,
659: 6,
660: 5,
661: 6,
662: 7,
663: 5,
664: 6,
665: 7,
666: 5,
667: 6,
668: 7,
669: 6,
670: 7,
671: 8,
672: 6,
673: 7,
674: 8,
675: 5,
676: 6,
677: 7,
678: 7,
679: 8,
680: 9,
681: 6,
682: 7,
683: 8,
684: 4,
685: 5,
686: 6,
687: 7,
688: 8,
689: 9,
690: 5,
691: 6,
692: 7,
693: 5,
694: 6,
695: 7,
696: 6,
697: 7,
698: 8,
699: 6,
700: 7,
701: 8,
702: 6,
703: 7,
704: 8,
705: 7,
706: 8,
707: 9,
708: 7,
709: 8,
710: 9,
711: 6,
712: 7,
713: 8,
714: 8,
715: 9,
716: 10,
717: 7,
718: 8,
719: 9,
720: 5,
721: 6,
722: 7,
723: 8,
724: 9,
725: 10,
726: 6,
727: 7,
728: 8}

import math
N = int(input())

def rec(N, num, A):
    if N <= 728:
        A.append(num + dic[N])
    else:
        p = int(math.log(N) / math.log(6))
        rec(N - 6**p, num+1, A)
        p = int(math.log(N) / math.log(9))
        rec(N - 9**p, num+1, A)
    return A

A = rec(N, 0, [])
print(min(A))

for i in range(1, 729):
    A = rec(i, 0, [])
    print('{}: {},'.format(i, min(A)))