Gegeven twee gehele getallen N jaar K de taak is om de som van modulo K van de eerste N natuurlijke getallen te vinden, dat wil zeggen 1% K + 2% K + ..... + N% K.
Voorbeelden:
Input : N = 10 and K = 2. Output : 5 Sum = 1%2 + 2%2 + 3%2 + 4%2 + 5%2 + 6%2 + 7%2 + 8%2 + 9%2 + 10%2 = 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 = 5.Recommended Practice Omgekeerde codering Probeer het!
Methode 1:
Herhaal een variabele i van 1 tot N, evalueer en voeg i%K toe.
Hieronder vindt u de implementatie van deze aanpak:
C++// C++ program to find sum of // modulo K of first N natural numbers. #include using namespace std; // Return sum of modulo K of // first N natural numbers. int findSum(int N int K) { int ans = 0; // Iterate from 1 to N && // evaluating and adding i % K. for (int i = 1; i <= N; i++) ans += (i % K); return ans; } // Driver Program int main() { int N = 10 K = 2; cout << findSum(N K) << endl; return 0; }
Java // Java program to find sum of modulo // K of first N natural numbers. import java.io.*; class GFG { // Return sum of modulo K of // first N natural numbers. static int findSum(int N int K) { int ans = 0; // Iterate from 1 to N && evaluating // and adding i % K. for (int i = 1; i <= N; i++) ans += (i % K); return ans; } // Driver program static public void main(String[] args) { int N = 10 K = 2; System.out.println(findSum(N K)); } } // This code is contributed by vt_m.
Python3 # Python3 program to find sum # of modulo K of first N # natural numbers. # Return sum of modulo K of # first N natural numbers. def findSum(N K): ans = 0; # Iterate from 1 to N && # evaluating and adding i % K. for i in range(1 N + 1): ans += (i % K); return ans; # Driver Code N = 10; K = 2; print(findSum(N K)); # This code is contributed by mits
C# // C# program to find sum of modulo // K of first N natural numbers. using System; class GFG { // Return sum of modulo K of // first N natural numbers. static int findSum(int N int K) { int ans = 0; // Iterate from 1 to N && evaluating // and adding i % K. for (int i = 1; i <= N; i++) ans += (i % K); return ans; } // Driver program static public void Main() { int N = 10 K = 2; Console.WriteLine(findSum(N K)); } } // This code is contributed by vt_m.
PHP // PHP program to find sum // of modulo K of first N // natural numbers. // Return sum of modulo K of // first N natural numbers. function findSum($N $K) { $ans = 0; // Iterate from 1 to N && // evaluating and adding i % K. for ($i = 1; $i <= $N; $i++) $ans += ($i % $K); return $ans; } // Driver Code $N = 10; $K = 2; echo findSum($N $K) 'n'; // This code is contributed by ajit ?> JavaScript <script> // JavaScript program to find sum // of modulo K of first N natural // numbers. // Return sum of modulo K of // first N natural numbers. function findSum(N K) { let ans = 0; // Iterate from 1 to N && evaluating // and adding i % K. for(let i = 1; i <= N; i++) ans += (i % K); return ans; } // Driver Code let N = 10 K = 2; document.write(findSum(N K)); // This code is contributed by code_hunt </script>
Uitgang:
5
Tijdcomplexiteit: OP).
Hulpruimte: O(1)
Methode 2:
Bij deze methode doen zich twee gevallen voor.
Geval 1: Wanneer N< K voor elk getal i N >= i >= 1 geeft i als resultaat als je werkt met modulo K. De vereiste som zal dus de som zijn van het eerste N natuurlijke getal N*(N+1)/2.
Geval 2: Wanneer N >= K dan zullen gehele getallen van 1 tot K in de reeks natuurlijke getallen 1 2 3 opleveren ... K - 1 0 als resultaat wanneer ze werken met modulo K. Op dezelfde manier zal het van K + 1 tot 2K hetzelfde resultaat opleveren. Het idee is dus om te tellen hoe vaak deze reeks voorkomt en deze te vermenigvuldigen met de som van de eerste K - 1 natuurlijke getallen.
Hieronder vindt u de implementatie van deze aanpak:
C++// C++ program to find sum of modulo // K of first N natural numbers. #include using namespace std; // Return sum of modulo K of // first N natural numbers. int findSum(int N int K) { int ans = 0; // Counting the number of times 1 2 .. // K-1 0 sequence occurs. int y = N / K; // Finding the number of elements left which // are incomplete of sequence Leads to Case 1 type. int x = N % K; // adding multiplication of number of // times 1 2 .. K-1 0 sequence occurs // and sum of first k natural number and sequence // from case 1. ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2; return ans; } // Driver program int main() { int N = 10 K = 2; cout << findSum(N K) << endl; return 0; }
Java // Java program to find sum of modulo // K of first N natural numbers. import java.io.*; class GFG { // Return sum of modulo K of // first N natural numbers. static int findSum(int N int K) { int ans = 0; // Counting the number of times 1 2 .. // K-1 0 sequence occurs. int y = N / K; // Finding the number of elements left which // are incomplete of sequence Leads to Case 1 type. int x = N % K; // adding multiplication of number of times // 1 2 .. K-1 0 sequence occurs and sum // of first k natural number and sequence // from case 1. ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2; return ans; } // Driver program static public void main(String[] args) { int N = 10 K = 2; System.out.println(findSum(N K)); } } // This Code is contributed by vt_m.
Python3 # Python3 program to find sum of modulo # K of first N natural numbers. # Return sum of modulo K of # first N natural numbers. def findSum(N K): ans = 0; # Counting the number of times # 1 2 .. K-1 0 sequence occurs. y = N / K; # Finding the number of elements # left which are incomplete of # sequence Leads to Case 1 type. x = N % K; # adding multiplication of number # of times 1 2 .. K-1 0 # sequence occurs and sum of # first k natural number and # sequence from case 1. ans = ((K * (K - 1) / 2) * y + (x * (x + 1)) / 2); return int(ans); # Driver Code N = 10; K = 2; print(findSum(N K)); # This code is contributed by mits
C# // C# program to find sum of modulo // K of first N natural numbers. using System; class GFG { // Return sum of modulo K of // first N natural numbers. static int findSum(int N int K) { int ans = 0; // Counting the number of times 1 2 .. // K-1 0 sequence occurs. int y = N / K; // Finding the number of elements left which // are incomplete of sequence Leads to Case 1 type. int x = N % K; // adding multiplication of number of times // 1 2 .. K-1 0 sequence occurs and sum // of first k natural number and sequence // from case 1. ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2; return ans; } // Driver program static public void Main() { int N = 10 K = 2; Console.WriteLine(findSum(N K)); } } // This code is contributed by vt_m.
PHP // PHP program to find sum of modulo // K of first N natural numbers. // Return sum of modulo K of // first N natural numbers. function findSum($N $K) { $ans = 0; // Counting the number of times // 1 2 .. K-1 0 sequence occurs. $y = $N / $K; // Finding the number of elements // left which are incomplete of // sequence Leads to Case 1 type. $x = $N % $K; // adding multiplication of number // of times 1 2 .. K-1 0 // sequence occurs and sum of // first k natural number and // sequence from case 1. $ans = ($K * ($K - 1) / 2) * $y + ($x * ($x + 1)) / 2; return $ans; } // Driver program $N = 10; $K = 2; echo findSum($N $K) ; // This code is contributed by anuj_67. ?> JavaScript <script> // Javascript program to find sum of modulo // K of first N natural numbers. // Return sum of modulo K of // first N natural numbers. function findSum(N K) { let ans = 0; // Counting the number of times // 1 2 .. K-1 0 sequence occurs. let y = N / K; // Finding the number of elements // left which are incomplete of // sequence Leads to Case 1 type. let x = N % K; // adding multiplication of number // of times 1 2 .. K-1 0 // sequence occurs and sum of // first k natural number and // sequence from case 1. ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2; return ans; } // Driver code let N = 10; let K = 2; document.write(findSum(N K)); // This code is contributed by _saurabh_jaiswal </script>
Uitgang:
5
Tijdcomplexiteit: O(1).
Hulpruimte: O(1)