add without carry

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Fri Sep 15 18:25:23 EDT 2006


Jon Ribbens a écrit :
> In article <450a7dec$0$14661$626a54ce at news.free.fr>, Bruno Desthuilliers wrote:
> 
>>Hugh wrote:
>>
>>>Sorry, here's an example...
>>>
>>>5+7=12
>>>
>>>added without carrying, 5+7=2
>>>
>>>i.e the result is always less than 10
>>
>>>I've been thinking some more about this and my brain is starting to
>>>work something out... 
>>
>>No need to think too long to come up with the most possibly Q&D solution:
>>
>>res = int(str(5 + 7)[-1])
> 
> 
> Am I missing something subtle in the question or is there some reason
> that nobody has posted the correct solution:
> 
>   (a + b) % 10

I'm afraid Peter Otten did a couple hours before you. But please note 
that my solution *is* actually 100% correct (base 10 assumed). It may be 
totally inefficiant, but that's another problem !-)



More information about the Python-list mailing list