-2

If one has a list of very very large numbers in Python, so much so that the compiler cannot get the value of them as numbers. Is there a function to sort this list (while keeping the numbers as integers) in a more efficient way such as comparing number to number? (But without converting to string)

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
rassa45
  • 3,482
  • 1
  • 29
  • 43
  • 1
    Python handles large numbers without problems. Try `1 << 1000`. – dlask Jun 01 '15 at 04:06
  • How large are your numbers? The sys.maxsize + 1 should be promoted to long, and the amount of available address space is a limit for your numbers. – Boris Jun 01 '15 at 04:07
  • What compiler? Python can handle arbitrarily large integers. Your limit is memory and CPU time... – dawg Jun 01 '15 at 04:11
  • Do you have any examples of these numbers you can including in your question? Please include them as part of solid: http://stackoverflow.com/help/mcve – localhost Jun 01 '15 at 04:17

2 Answers2

7

Both Python 2 and Python 3 handle arbitrarily large integers:

>>> [2**34, 2**38, 2**99, 2**122, 2]
[17179869184, 274877906944, 633825300114114700748351602688L, 5316911983139663491615228241121378304L, 2]

And sort them as expected:

>>> sorted(_)
[2, 17179869184, 274877906944, 633825300114114700748351602688L, 5316911983139663491615228241121378304L]

(Python 2 will show a L for integers larger than sys.maxint as in the example here while Python 3 shows the integer values the same for smaller or larger integers)

dawg
  • 98,345
  • 23
  • 131
  • 206
2

This would be a solution that works for other languages. You could have a list of list of integers. So basically, given a number like 123456789, you could represent it as

bigNum = [[1,2,3],[4,5,6],[7,8,9]]

Comparing big numbers could be done by the following:

  • Compare the number of digits
  • If the number of digits are equal, compare the numbers starting from the largest digit.
Sean Saito
  • 655
  • 7
  • 18