Project Euler #13: Large sum

Sort by

recency

|

159 Discussions

|

  • + 0 comments

    Python is not fun :(

    print(str(sum([int(input()) for _ in range(int(input()))]))[:10])
    
  • + 0 comments

    Haskell

    import Control.Applicative
    import Control.Monad
    import System.IO
    import Prelude
    
    main :: IO ()
    main = do
        t_temp <- getLine
        let t = read t_temp :: Int
        n_temp <- getMultipleLines t
        let n = map (read :: String -> Integer) n_temp :: [Integer]
        let summed = sum n
        print(read (take 10 (show summed)) :: Int)
    
    
    getMultipleLines :: Int -> IO [String]
    getMultipleLines n
        | n <= 0 = return []
        | otherwise = do          
            x <- getLine         
            xs <- getMultipleLines (n-1)    
            let ret = (x:xs)    
            return ret
            
    
  • + 0 comments

    Python3

    if name == 'main':

    T = int(input())
    total_sum = 0
    
    for _ in range(T):
        n = int(input())
        total_sum += n
    
    
    print(str(total_sum)[:10])
    
  • + 0 comments

    Java8 code

    import java.io.*;
    import java.util.*;
    import java.math.BigInteger;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int testcases = s.nextInt();  // Number of test cases
            BigInteger sum = BigInteger.ZERO;
    
            // Summing up all the large numbers
            for (int i = 0; i < testcases; i++) {
                String num = s.next();  // Read the number as a string
                sum = sum.add(new BigInteger(num));  // Add to the sum using BigInteger
            }
    
            // Convert the sum to a string and print the first 10 digits
            String result = sum.toString();
            System.out.println(result.substring(0, 10));
        }
    }
    
  • + 0 comments

    100% python

    n = int(input())
    ans = 0
    for _ in range(n):
        num = int(input())
        ans += num
    
    print(str(ans)[:10])