Chapter 5 (Part 1) - Practice

Q1: Population Density

  • اعمل قاعدة بيانات للسكان والمساحة للدول دي:
Country Population (in million people) Area (in millions of square miles)
Usa 203 3
India 548 1
China 800 4
Brazil 108 3
  • وبعدين اعمل rule:

  • density is population divided by area.

  • واكتب الاستعلامات:

  1. Find the population of India.
  2. Find the area of Brazil.
  3. Find the population density of China.

Q2: Arithmetic Functions

  • اعمل rules للدوال الرياضية دي:
  1. Sum: S = X + Y
  2. Calculate: y = (x + 3)²
  3. Polynomial: F = X⁴ + 2X + 5
  • واكتب الاستعلامات:
  • Find the sum of 5 and 4.
  • Find y when x = 2 using calculate.
  • Find F when X = 3 using the polynomial.

Q3: Factorial

اعمل recursive rule لحساب factorial:

  • Base: factorial of 1 is 1.
  • Inductive: factorial of N is N × factorial of N-1.

واكتب الاستعلام:

  • Find the factorial of 5.

Q4: GCD

  • اعمل recursive rule لحساب greatest common divisor (GCD):
  1. If X and Y are equal, D = X.
  2. If X < Y, D = gcd(X, Y - X).
  3. If Y < X, D = gcd(Y, X).
  • واكتب الاستعلامات:
  • Find the GCD of 12 and 8.
  • Find the GCD of 100 and 35.

Q5: Logic Gates

  • اعمل Facts للبوابات المنطقية الأساسية (not, and, or, xor):
Inputs and or xor
0, 0 0 0 0
0, 1 0 1 1
1, 0 0 1 1
1, 1 1 1 0
Inputs not
0 1
1 0

Q6: Half Adder

  • استخدم "and" و "xor" من Q5 عشان تبني half-adder:

  • Carry = A and B

  • Sum = A xor B

  • وجرب inputs (0,1) و (1,1).


Q7: Boolean Function

  • استخدم "not", "and", "or" من Q5 عشان تعمل rule للدالة:

  • F = B' + AC

  • يعني: not B, then and(A, C), then or the results.

  • وجرب inputs:

  • A=1, B=0, C=1

  • A=1, B=1, C=1

  • A=0, B=1, C=1

  • A=0, B=0, C=0


Q8: Summation 1 to X

  • اعمل recursive rule تجمع الأرقام من 1 لأي رقم X:

  • F = 1 + 2 + 3 + ... + X

  • واكتب الاستعلامات:

  • Sum from 1 to 5.

  • Sum from 1 to 10.

  • Sum from 1 to 100.


Q9: Summation of Odd Numbers 1 to M

  • اعمل recursive rule تجمع الأرقام الفردية (odd) من 1 لأي رقم فردي M:

  • لو M زوجي: تخطاه

    • Base: sum_odd(1, 1).

واكتب الاستعلامات:

  • Sum of odd numbers from 1 to 5.
  • Sum of odd numbers from 1 to 7.
  • Sum of odd numbers from 1 to 10.

Nour Eldeen Mahmoud