circle,Rational Number

import math

class Circle:
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return math.pi * self.radius * self.radius

    def perimeter(self):
        return 2 * math.pi * self.radius
    

c1 = Circle(7)
print('Area:',c1.area())
print('Perimeter:',c1.perimeter())

1. What is a Rational Number?

rational number is any number that can be expressed as a fraction where both the numerator and the denominator are integers (whole numbers), and the denominator is not zero.

The key idea is ratio. The word “rational” comes from the word “ratio.”

General Form:
a / b

  • a is the numerator (an integer)
  • b is the denominator (an integer, and b ≠ 0)

Examples:

  • 1/23/4-5/7 (Simple fractions)
  • 5 (because it can be written as 5/1)
  • 0 (because it can be written as 0/1)
  • -3 (because it can be written as -3/1)
  • 0.75 (because it can be written as 3/4)
  • 0.333... (because it can be written as 1/3)

Non-Examples:

  • π (Pi) – It cannot be expressed as a simple fraction of two integers. Its decimal expansion is non-terminating and non-repeating.
  • √2 (Square root of 2) – It also cannot be expressed as a simple fraction of two integers.

2. Formulas for Addition and Subtraction

The most important rule for adding and subtracting rational numbers is that they must have a common denominator.

Let our two rational numbers be:
a / b and c / d
where a, b, c, d are integers, and b ≠ 0d ≠ 0.

Formula for Addition:

(a/b) + (c/d) = (a×d + c×b) / (b×d)

Step-by-step reasoning:

  1. Find a common denominator. The easiest one is the product of the two denominators: b × d.
  2. Adjust the first fraction: (a/b) becomes (a×d)/(b×d).
  3. Adjust the second fraction: (c/d) becomes (c×b)/(b×d).
  4. Now that the denominators are the same, add the numerators: (a×d + c×b).
  5. Place the result over the common denominator.

Example: Add 1/2 and 1/3

  • a=1, b=2, c=1, d=3
  • Using the formula: (1/2) + (1/3) = (1×3 + 1×2) / (2×3) = (3 + 2) / 6 = 5/6

Formula for Subtraction:

(a/b) – (c/d) = (a×d – c×b) / (b×d)

The process is identical to addition, except you subtract the numerators.

Step-by-step reasoning:

  1. Find the common denominator: b × d.
  2. Adjust the first fraction: (a×d)/(b×d).
  3. Adjust the second fraction: (c×b)/(b×d).
  4. Subtract the numerators: (a×d - c×b).
  5. Place the result over the common denominator.

Example: Subtract 1/3 from 1/2

  • a=1, b=2, c=1, d=3
  • Using the formula: (1/2) - (1/3) = (1×3 - 1×2) / (2×3) = (3 - 2) / 6 = 1/6

Important Note: Simplifying the Result

After you calculate the sum or difference, always check if the resulting fraction can be simplified.

For example, if you add 1/4 + 1/4:

  • (1×4 + 1×4) / (4×4) = (4+4)/16 = 8/16
  • The result 8/16 can be simplified by dividing the numerator and denominator by their greatest common divisor (8), giving you the final answer: 1/2.

Summary

OperationFormulaExample
Addition(a/b) + (c/d) = (ad + bc)/(bd)1/2 + 1/3 = (3+2)/6 = 5/6
Subtraction(a/b) - (c/d) = (ad - bc)/(bd)1/2 - 1/3 = (3-2)/6 = 1/6

class Rational:

    def __init__(self, p, q):
        self.p = p
        self.q = q

    def __add__(self, other):
        p = self.p * other.q + self.q * other.p
        q = self.q * other.q
        sum = Rational(p,q)
        return sum

    def __sub__(self, other):
        p = self.p * other.q - self.q * other.p
        q = self.q * other.q
        sum = Rational(p, q)
        return sum

    def __str__(self):
        return str(self.p) + '/' + str(self.q)


r1 = Rational(2,3)
r2 = Rational(1,2)

r3 = r1 + r2

print(r1, '+', r2, '=', r3)

Similar Posts

  • Functions as Parameters in Python

    Functions as Parameters in Python In Python, functions are first-class objects, which means they can be: Basic Concept When we pass a function as a parameter, we’re essentially allowing one function to use another function’s behavior. Simple Examples Example 1: Basic Function as Parameter python def greet(name): return f”Hello, {name}!” def farewell(name): return f”Goodbye, {name}!” def…

  • Basic Character Classes

    Basic Character Classes Pattern Description Example Matches [abc] Matches any single character in the brackets a, b, or c [^abc] Matches any single character NOT in the brackets d, 1, ! (not a, b, or c) [a-z] Matches any character in the range a to z a, b, c, …, z [A-Z] Matches any character in the range A to Z A, B, C, …, Z [0-9] Matches…

  • Class Variables Andmethds

    Class Variables Class variables are variables that are shared by all instances of a class. They are defined directly within the class but outside of any method. Unlike instance variables, which are unique to each object, a single copy of a class variable is shared among all objects of that class. They are useful for…

  • Password Strength Checker

    python Enhanced Password Strength Checker python import re def is_strong(password): “”” Check if a password is strong based on multiple criteria. Returns (is_valid, message) tuple. “”” # Define criteria and error messages criteria = [ { ‘check’: len(password) >= 8, ‘message’: “at least 8 characters” }, { ‘check’: bool(re.search(r'[A-Z]’, password)), ‘message’: “one uppercase letter (A-Z)”…

  • Mutable vs. Immutable Objects in Python 🔄🔒

    Mutable vs. Immutable Objects in Python 🔄🔒 In Python, mutability determines whether an object’s value can be changed after creation. This is crucial for understanding how variables behave. 🤔 Immutable Objects 🔒 Example 1: Strings (Immutable) 💬 Python Example 2: Tuples (Immutable) 📦 Python Mutable Objects 📝 Example 1: Lists (Mutable) 📋 Python Example 2:…

  • Lambda Functions in Python

    Lambda Functions in Python Lambda functions are small, anonymous functions defined using the lambda keyword. They can take any number of arguments but can only have one expression. Basic Syntax python lambda arguments: expression Simple Examples 1. Basic Lambda Function python # Regular function def add(x, y): return x + y # Equivalent lambda function add_lambda =…

Leave a Reply

Your email address will not be published. Required fields are marked *