Problem Solving और Algorithms
Problem solving क्या है?
Computer ख़ुद नहीं सोच सकता। उससे कोई problem solve कराने के लिए — bill बनाना, topper ढूँढना, ticket book करना — पहले हमें problem समझकर ठीक-ठीक steps निकालने होते हैं। फिर हम वे steps किसी programming language में लिखते हैं। Problem पहचानने, solution design करने और उसे चलने वाले program में बदलने की पूरी process को problem solving कहते हैं।
1. 1 cup पानी उबालें। 2. चायपत्ती और चीनी डालें। 3. दूध डालकर 2 minute उबालें। 4. Cup में छानें। क्रम बदल दें (उबालने से पहले छान लें) तो result ग़लत — algorithm में भी क्रम ज़रूरी है।
Problem solving के steps
Example: "5 subjects के marks से student का percentage निकालें।"
- Input: 5 marks (हर एक 100 में से)।
- Process: total = marks का जोड़; percentage = total ÷ 500 × 100।
- Output: total और percentage।
Algorithm क्या है?
Algorithm किसी problem को solve करने के well-defined steps का सीमित क्रम है। यह शब्द Persian गणितज्ञ al-Khwarizmi के नाम से आया है। Algorithm किसी programming language पर निर्भर नहीं — वही algorithm Python, C++ या Java में लिखा जा सकता है।
अच्छे algorithm की विशेषताएँ
Algorithm examples
Example 1: Rectangle का area
- Start
- Length L और breadth B input करें
- Area ← L × B
- Area print करें
- Stop
Example 2: Number even है या odd
- Start
- N input करें
- अगर N mod 2 = 0 तो "Even" print करें, नहीं तो "Odd"
- Stop
Example 3: तीन numbers में सबसे बड़ा
- Start
- A, B, C input करें
- Largest ← A
- अगर B > Largest तो Largest ← B
- अगर C > Largest तो Largest ← C
- Largest print करें
- Stop
Example 4: N का factorial (N! = 1 × 2 × … × N)
- Start
- N input करें
- Fact ← 1, i ← 1
- जब तक i ≤ N: Fact ← Fact × i; i ← i + 1
- Fact print करें
- Stop
Algorithm verify करना (dry run)
Coding से पहले हम algorithm को dry run से verify करते हैं: हम ख़ुद computer बनकर sample input लेते हैं और हर variable की value trace table में लिखते हैं। Example 4 का N = 4 पर dry run:
| Step | i | i ≤ N ? | Fact |
|---|---|---|---|
| start | 1 | — | 1 |
| loop 1 | 1 | True | 1 × 1 = 1 |
| loop 2 | 2 | True | 1 × 2 = 2 |
| loop 3 | 3 | True | 2 × 3 = 6 |
| loop 4 | 4 | True | 6 × 4 = 24 |
| check | 5 | False → exit | 24 |
Special cases भी test करें: N = 0 पर 1 आना चाहिए (loop नहीं चलता, Fact 1 ही रहता है ✔), और N = 1 पर 1 ✔।
Algorithms की तुलना
Problem: 1 + 2 + … + N का जोड़। दो सही algorithms:
Algorithm A – loop
- total ← 0
- for i = 1 to N: total ← total + i
- N additions
- N = 10 लाख → 10 लाख steps
Algorithm B – formula
- total ← N × (N + 1) ÷ 2
- सिर्फ़ 3 operations
- किसी भी N पर same speed
- साफ़ तौर पर ज़्यादा efficient
def sum_loop(n): # Algorithm A: add one by one
total = 0
for i in range(1, n + 1):
total += i
return total
def sum_formula(n): # Algorithm B: Gauss formula
return n * (n + 1) // 2
n = 1000000
print(sum_loop(n), sum_formula(n))
print('Loop steps:', n, '| Formula steps: 1')एक और example: N prime है या नहीं, यह जाँचने के लिए N − 1 तक नहीं, सिर्फ़ √N तक के divisors test करने होते हैं। N = 10,007 के लिए यह 10,000 की जगह लगभग 100 checks है।
Algorithm से code तक
Example 3 Python में। हर line सीधे एक algorithm step से जुड़ती है।
a = int(input('First number: '))
b = int(input('Second number: '))
c = int(input('Third number: '))
largest = a
if b > largest:
largest = b
if c > largest:
largest = c
print('Largest =', largest)सारांश और practice
- Problem solving: analyse → algorithm → code → test और debug।
- Algorithm = input और output वाले सीमित, साफ़, effective steps।
- Dry run और trace table से verify करें; special cases test करें।
- Algorithms की तुलना steps (time) और memory से करें।
Algorithm लिखें: (1) Celsius को Fahrenheit में बदलना (F = C × 9/5 + 32), (2) साल leap year है या नहीं। (2) का 1900, 2000 और 2024 के साथ dry run करें।
Leap year hint: 400 से divisible हो, या 4 से divisible हो पर 100 से नहीं। 1900 → leap नहीं, 2000 → leap, 2024 → leap।
