Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and Julia computes functions in different way?

Python 3 computation for

N=123456789
sum(map(int,str(N))            ---> 45

Julia 0.6.2 computation for

N = 123456789
sum([Int(ch) for ch in "$N"])  ---> 477

N = 123456789
sum(map(Int, collect("$N")))  --->  477

Why is this?

like image 206
Lijo Joseph Avatar asked Dec 24 '22 07:12

Lijo Joseph


1 Answers

Because the python equivalent to your Julia-Statement would be

N=123456789
print( sum(map(ord,str(N))))

Output:

477

It is summing up the ascii-ord-value of '1'+'2'+...'9' - not converting each char of the string into an int and then summing up the ints.

This is not the same:

N=123456789
sum(map(int,str(N))

it converts the long int into a string, feeds each charachter into int() which converts '1' back to 1 (not to ord('1')) and then adds the numbers to 45

like image 120
Patrick Artner Avatar answered Dec 25 '22 20:12

Patrick Artner