בהינתן מספר שלם N מצא והראה את מספר הזוגות שעומד בתנאים הבאים:
- ריבוע המרחק בין שני המספרים האלה שווה ל- LCM מבין שני המספרים הללו.
- ה GCD של שני המספרים האלה שווה למכפלת שני מספרים שלמים עוקבים.
- שני המספרים בזוג צריכים להיות קטנים או שווים ל-N.
פֶּתֶק: יש להציג רק את הזוגות שמקיימים את שני התנאים שלעיל בו-זמנית ומספרים אלה חייבים להיות קטנים או שווים ל-N.
דוגמאות:
Input: 10 Output: No. of pairs = 1 Pair no. 1 --> (2 4) Input: 500 Output: No. of pairs = 7 Pair no. 1 --> (2 4) Pair no. 2 --> (12 18) Pair no. 3 --> (36 48) Pair no. 4 --> (80 100) Pair no. 5 --> (150 180) Pair no. 6 --> (252 294) Pair no. 7 --> (392 448)
הֶסבֵּר:
הטבלאות המוצגות להלן יתנו מבט ברור של מה שניתן למצוא:

כיצד להשבית את מצב מפתח
הטבלאות שלמעלה מציגות GCD שנוצר על ידי מכפלה של שני מספרים עוקבים וכפולות התואמות שלו שבהן קיים UNIQUE PAIR המקביל לכל ערך. ערכים ירוקים בכל שורה יוצרים זוג ייחודי עבור GCD תואם.
פֶּתֶק: בטבלאות לעיל
- עבור כניסה ראשונה GCD=2 ראשון וכפולה שניה של 2 יוצרים את הזוג הייחודי (2 4)
- באופן דומה עבור הערך השני GCD=6 2nd וכפולה השלישית של 6 יוצרים את הזוג הייחודי (12 18)
- באופן דומה עוברים לכניסה Zth, כלומר עבור GCD = Z*(Z+1), ברור שהזוג הייחודי יכלול מכפילה Zth ו-(Z+1)th של GCD = Z*(Z+1). כעת הכפולה Z של GCD היא Z * (Z*(Z+1)) וכפולה Z+1 של GCD תהיה (Z + 1) * (Z*(Z+1)).
- ומכיוון שהמגבלה היא N כך המספר השני בזוג הייחודי חייב להיות קטן או שווה ל-N. אז (Z + 1) * (Z*(Z+1))<= N. Simplifying it further the desired relation is derived Z3+ (2*Z2) + Z<=N
זה יוצר דפוס ומהחישוב המתמטי נגזר שעבור N נתון המספר הכולל של זוגות ייחודיים כאלה (נניח Z) יעקוב אחר יחס מתמטי המוצג להלן:
Z3 + (2*Z2) + Z <= N
להלן היישום הנדרש:
// C program for finding the required pairs #include #include // Finding the number of unique pairs int No_Of_Pairs(int N) { int i = 1; // Using the derived formula while ((i * i * i) + (2 * i * i) + i <= N) i++; return (i - 1); } // Printing the unique pairs void print_pairs(int pairs) { int i = 1 mul; for (i = 1; i <= pairs; i++) { mul = i * (i + 1); printf('Pair no. %d --> (%d %d)n' i (mul * i) mul * (i + 1)); } } // Driver program to test above functions int main() { int N = 500 pairs mul i = 1; pairs = No_Of_Pairs(N); printf('No. of pairs = %d n' pairs); print_pairs(pairs); return 0; }
Java // Java program for finding // the required pairs import java.io.*; class GFG { // Finding the number // of unique pairs static int No_Of_Pairs(int N) { int i = 1; // Using the derived formula while ((i * i * i) + (2 * i * i) + i <= N) i++; return (i - 1); } // Printing the unique pairs static void print_pairs(int pairs) { int i = 1 mul; for (i = 1; i <= pairs; i++) { mul = i * (i + 1); System.out.println('Pair no. ' + i + ' --> (' + (mul * i) + ' ' + mul * (i + 1) + ')'); } } // Driver code public static void main (String[] args) { int N = 500 pairs mul i = 1; pairs = No_Of_Pairs(N); System.out.println('No. of pairs = ' + pairs); print_pairs(pairs); } } // This code is contributed by Mahadev.
Python3 # Python3 program for finding the required pairs # Finding the number of unique pairs def No_Of_Pairs(N): i = 1; # Using the derived formula while ((i * i * i) + (2 * i * i) + i <= N): i += 1; return (i - 1); # Printing the unique pairs def print_pairs(pairs): i = 1; mul = 0; for i in range(1 pairs + 1): mul = i * (i + 1); print('Pair no.' i ' --> (' (mul * i) ' ' mul * (i + 1) ')'); # Driver Code N = 500; i = 1; pairs = No_Of_Pairs(N); print('No. of pairs = ' pairs); print_pairs(pairs); # This code is contributed # by mits
C# // C# program for finding // the required pairs using System; class GFG { // Finding the number // of unique pairs static int No_Of_Pairs(int N) { int i = 1; // Using the derived formula while ((i * i * i) + (2 * i * i) + i <= N) i++; return (i - 1); } // Printing the unique pairs static void print_pairs(int pairs) { int i = 1 mul; for (i = 1; i <= pairs; i++) { mul = i * (i + 1); Console.WriteLine('Pair no. ' + i + ' --> (' + (mul * i) + ' ' + mul * (i + 1) + ')'); } } // Driver code static void Main() { int N = 500 pairs; pairs = No_Of_Pairs(N); Console.WriteLine('No. of pairs = ' + pairs); print_pairs(pairs); } } // This code is contributed by mits
PHP // PHP program for finding // the required pairs // Finding the number // of unique pairs function No_Of_Pairs($N) { $i = 1; // Using the // derived formula while (($i * $i * $i) + (2 * $i * $i) + $i <= $N) $i++; return ($i - 1); } // Printing the unique pairs function print_pairs($pairs) { $i = 1; $mul; for ($i = 1; $i <= $pairs; $i++) { $mul = $i * ($i + 1); echo 'Pair no.' $i ' --> (' ($mul * $i) ' ' $mul * ($i + 1)') n'; } } // Driver Code $N = 500; $pairs; $mul; $i = 1; $pairs = No_Of_Pairs($N); echo 'No. of pairs = ' $pairs ' n'; print_pairs($pairs); // This code is contributed // by Akanksha Rai(Abby_akku) ?> JavaScript <script> // Javascript program for finding the // required pairs // Finding the number of unique pairs function No_Of_Pairs(N) { let i = 1; // Using the derived formula while ((i * i * i) + (2 * i * i) + i <= N) i++; return (i - 1); } // Printing the unique pairs function print_pairs(pairs) { let i = 1 mul; for(i = 1; i <= pairs; i++) { mul = i * (i + 1); document.write('Pair no. ' + i + ' --> (' + (mul * i) + ' ' + mul * (i + 1) + ')
'); } } // Driver code let N = 500 pairs mul i = 1; pairs = No_Of_Pairs(N); document.write('No. of pairs = ' + pairs + '
'); print_pairs(pairs); // This code is contributed by mohit kumar 29 </script>
C++14 // C++ code for the above approach: #include using namespace std; // Finding the number of unique pairs int No_Of_Pairs(int N) { int i = 1; // Using the derived formula while ((i * i * i) + (2 * i * i) + i <= N) i++; return (i - 1); } // Printing the unique pairs void print_pairs(int pairs) { int i = 1 mul; for (i = 1; i <= pairs; i++) { mul = i * (i + 1); cout << 'Pair no. '<< i <<' --> (' << (mul * i) << ' '<< mul * (i + 1) << ')' <<endl;; } } // Driver Code int main() { int N = 500 pairs mul i = 1; pairs = No_Of_Pairs(N); cout << 'No. of pairs = ' << pairs << endl; print_pairs(pairs); return 0; }
תְפוּקָה:
No. of pairs = 7 Pair no. 1 --> (2 4) Pair no. 2 --> (12 18) Pair no. 3 --> (36 48) Pair no. 4 --> (80 100) Pair no. 5 --> (150 180) Pair no. 6 --> (252 294) Pair no. 7 --> (392 448)
מורכבות הזמן : O(N1/3)
חלל עזר : O(1)