Skip to main content
Remove request for feedback.
Source Link
Martin R
  • 23.5k
  • 2
  • 36
  • 92

I managed to solve it using the Math formulas both provided by user JAD, MJOLKA and Emily L.

It's not pretty looking but tried to make it functional while retaining versatility as to modifications, a single method also means it can be re-used to determine different values since I left things a bit abstract and the behavior determined by the parameters sent.

class Solution {
     
    private static long mathSolver(long _endNumber){
        long _firstDivision = 0, _secondDivision = 0, _thirdDivision = 0;
        //We search for individual values
        _firstDivision = Division(_endNumber, 3, 1);
        _secondDivision = Division(_endNumber, 5, 5);
        _thirdDivision = Division(_endNumber, 15, 15);

        return _firstDivision + _secondDivision - _thirdDivision;
    }


    private static long Division(long _endNumber, int _divider, int _substracter){
        //We search for residues, if they dont exist we apply our own
        long _division = 0;
        if(_endNumber % _divider == 0){ 
            _division = (_endNumber - _substracter)/_divider;
        }
        else{
            _division = (_endNumber- (_endNumber % _divider))/_divider;
        }
        return (_divider*((_division)*((_division +1))/2));
    }

    static void Main(String[] args) {
        long t = Convert.ToInt64(Console.ReadLine());
        for(int a0 = 0; a0 < t; a0++){
            long n = Convert.ToInt64(Console.ReadLine());
            long _sum = mathSolver(n);
            Console.WriteLine(_sum);
        }
    }
}```

It was very entertaining and I will check for more challenges whenever I have the time for them! you learn a lot of math-related subjects to make your code efficient!

If possible, I would appreciate some feedback! thank you!

It was very entertaining and I will check for more challenges whenever I have the time for them! you learn a lot of math-related subjects to make your code efficient!

I managed to solve it using the Math formulas both provided by user JAD, MJOLKA and Emily L.

It's not pretty looking but tried to make it functional while retaining versatility as to modifications, a single method also means it can be re-used to determine different values since I left things a bit abstract and the behavior determined by the parameters sent.

class Solution {
     
    private static long mathSolver(long _endNumber){
        long _firstDivision = 0, _secondDivision = 0, _thirdDivision = 0;
        //We search for individual values
        _firstDivision = Division(_endNumber, 3, 1);
        _secondDivision = Division(_endNumber, 5, 5);
        _thirdDivision = Division(_endNumber, 15, 15);

        return _firstDivision + _secondDivision - _thirdDivision;
    }


    private static long Division(long _endNumber, int _divider, int _substracter){
        //We search for residues, if they dont exist we apply our own
        long _division = 0;
        if(_endNumber % _divider == 0){ 
            _division = (_endNumber - _substracter)/_divider;
        }
        else{
            _division = (_endNumber- (_endNumber % _divider))/_divider;
        }
        return (_divider*((_division)*((_division +1))/2));
    }

    static void Main(String[] args) {
        long t = Convert.ToInt64(Console.ReadLine());
        for(int a0 = 0; a0 < t; a0++){
            long n = Convert.ToInt64(Console.ReadLine());
            long _sum = mathSolver(n);
            Console.WriteLine(_sum);
        }
    }
}```

It was very entertaining and I will check for more challenges whenever I have the time for them! you learn a lot of math-related subjects to make your code efficient!

If possible, I would appreciate some feedback! thank you!

I managed to solve it using the Math formulas both provided by user JAD, MJOLKA and Emily L.

It's not pretty looking but tried to make it functional while retaining versatility as to modifications, a single method also means it can be re-used to determine different values since I left things a bit abstract and the behavior determined by the parameters sent.

class Solution {
     
    private static long mathSolver(long _endNumber){
        long _firstDivision = 0, _secondDivision = 0, _thirdDivision = 0;
        //We search for individual values
        _firstDivision = Division(_endNumber, 3, 1);
        _secondDivision = Division(_endNumber, 5, 5);
        _thirdDivision = Division(_endNumber, 15, 15);

        return _firstDivision + _secondDivision - _thirdDivision;
    }


    private static long Division(long _endNumber, int _divider, int _substracter){
        //We search for residues, if they dont exist we apply our own
        long _division = 0;
        if(_endNumber % _divider == 0){ 
            _division = (_endNumber - _substracter)/_divider;
        }
        else{
            _division = (_endNumber- (_endNumber % _divider))/_divider;
        }
        return (_divider*((_division)*((_division +1))/2));
    }

    static void Main(String[] args) {
        long t = Convert.ToInt64(Console.ReadLine());
        for(int a0 = 0; a0 < t; a0++){
            long n = Convert.ToInt64(Console.ReadLine());
            long _sum = mathSolver(n);
            Console.WriteLine(_sum);
        }
    }
}

It was very entertaining and I will check for more challenges whenever I have the time for them! you learn a lot of math-related subjects to make your code efficient!

Source Link

I managed to solve it using the Math formulas both provided by user JAD, MJOLKA and Emily L.

It's not pretty looking but tried to make it functional while retaining versatility as to modifications, a single method also means it can be re-used to determine different values since I left things a bit abstract and the behavior determined by the parameters sent.

class Solution {
     
    private static long mathSolver(long _endNumber){
        long _firstDivision = 0, _secondDivision = 0, _thirdDivision = 0;
        //We search for individual values
        _firstDivision = Division(_endNumber, 3, 1);
        _secondDivision = Division(_endNumber, 5, 5);
        _thirdDivision = Division(_endNumber, 15, 15);

        return _firstDivision + _secondDivision - _thirdDivision;
    }


    private static long Division(long _endNumber, int _divider, int _substracter){
        //We search for residues, if they dont exist we apply our own
        long _division = 0;
        if(_endNumber % _divider == 0){ 
            _division = (_endNumber - _substracter)/_divider;
        }
        else{
            _division = (_endNumber- (_endNumber % _divider))/_divider;
        }
        return (_divider*((_division)*((_division +1))/2));
    }

    static void Main(String[] args) {
        long t = Convert.ToInt64(Console.ReadLine());
        for(int a0 = 0; a0 < t; a0++){
            long n = Convert.ToInt64(Console.ReadLine());
            long _sum = mathSolver(n);
            Console.WriteLine(_sum);
        }
    }
}```

It was very entertaining and I will check for more challenges whenever I have the time for them! you learn a lot of math-related subjects to make your code efficient!

If possible, I would appreciate some feedback! thank you!