Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is division producing a negative number?

Why is this printing the negative number -147982099 instead of 8462696833 = 600851475143 / 71

import Data.List

smallFactor n = case (elemIndex 0 (map (mod n) [2..])) of
                    Just x -> x + 2

main = print( quot n (smallFactor n) )
    where n = 600851475143

The full output:

$ ghc --make p3; ./p3
[1 of 1] Compiling Main             ( p3.hs, p3.o )
Linking p3 ...
-147982099
like image 439
user782220 Avatar asked Oct 15 '13 23:10

user782220


People also ask

Why did we create negative numbers?

Negative numbers are used to describe values on a scale that goes below zero, such as the Celsius and Fahrenheit scales for temperature. The laws of arithmetic for negative numbers ensure that the common-sense idea of an opposite is reflected in arithmetic.


1 Answers

Because you are telling it a negative number (assuming you are using a 32 bit GHC).

where n = 600851475143 -- n = -443946297

notice:

Prelude Data.Int> 600851475143 :: Int32
-443946297
like image 103
Thomas M. DuBuisson Avatar answered Sep 19 '22 18:09

Thomas M. DuBuisson