Project Euler #1: Multiples of 3 and 5

  • + 0 comments

    KOTLIN Code

    • TC 2 & 3 checks if your code handles large numbers, use Long instead of Int.
    private fun calculateMultipleSums(n: Int){
        /*
            100/3 = 33
            100/5 = 20
            100/15 = 6        
        
            (n*(n+1))/2
        */
    		
        val sumOfThree = 3* findSum(findMultiplier(n, 3))
        val sumOfFive = 5* findSum(findMultiplier(n, 5))
        val sumOfFifteen = 15* findSum(findMultiplier(n, 15))
        
        println( sumOfThree +sumOfFive - sumOfFifteen)
    }
    
    private fun findSum(n: Long): Long{
        return (n * (n+1))/2
    }
    private fun findMultiplier(n: Int, m: Long): Long{
        return ((n-1)/m)
    }