You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 regels
1.0 KiB

  1. using System;
  2. namespace JobNimbus_application
  3. {
  4. public static class SumService
  5. {
  6. /*
  7. This method takes an array of integers and sums all the multiples of those integers from 0 to the value specified in 'range'
  8. */
  9. public static long sumMultiples(int[] multiples, int range){
  10. if(range < 0){
  11. Console.WriteLine("range must be positive");
  12. return -1;
  13. }
  14. if(multiples == null || multiples.Length == 0){
  15. Console.WriteLine("multiples must be provided");
  16. return -1;
  17. }
  18. long multipleSum = 0;
  19. for(int i = 0; i < range; i++){
  20. foreach(int multiple in multiples){
  21. if(i % multiple == 0){
  22. multipleSum += i;
  23. break; //Be sure not to double count values that are divisible by multiple multiples
  24. }
  25. }
  26. }
  27. return multipleSum;
  28. }
  29. }
  30. }