Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Getting Started with Competitive Programming in Python
Next article icon

Mathematics Tricks For Competitive Programming In Python 3

Last Updated : 13 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Here are some of the exciting features that Python 3.8 provides for programmers in competitive programming. These new features are related to some math functions and algorithms that are frequently used by competitive programmers. The implementation of these features has the time complexity which is the same in case of a program when implemented from scratch.
We will discuss 
 

  • Modular Exponentiation
  • Multiplicative Modulo Inverse
  • Calculating nCr and nPr

 

Modular Exponentiation

Given 3 numbers A, B and Mod.Calculate (AB)%Mod. 
Examples: 
 

Input : A = 4, B = 3 Mod = 11  Output : 9  Explanation: (43)%11 = (64)%11 = 9  Input : A = 3, B = 3 Mod = 5  Output : 2 Explanation: (33)%5 = (27)%5 = 2

The traditional Implementation of Modular Exponentiation is discussed here
Below is the discussed Python3.8 solution 
 

Python




A = 4
B = 3
Mod = 11
 
# Power function can take 3
# parameters and can compute
# (A ^ B)% Mod
print('The power is {}'.format(pow(A, B, Mod)))
 
 

Output: 
 

The power is 9

 

Modular Multiplicative Inverse

Given two integers A and Mod, Calculate Modular multiplicative inverse of A under modulo Mod. 
The modular multiplicative inverse is an integer B such that 
 

(A.B)%Mod = 1 where gcd(A, Mod) should be equal to 1

Examples: 
 

Input : A = 4, Mod = 11   Output : 3  Explanation: (4*3)%11 = (12)%11 = 1  Input : A = 3, Mod = 5  Output : 2 Explanation: (3*2)%5 = (6)%5 = 1

The traditional Implementation of Modular Multiplicative Inverse is discussed here
Below is the discussed Python3.8 solution
 

Python




A = 4
Mod = 11
 
# Power function can take 3 parameters
# and can compute (A^-1)% Mod
print(f'The Modular Multiplicative Inverse \
of A under Mod is {pow(A, -1, Mod)}')
 
 

Output: 
 

The Modular Multiplicative Inverse of A under Mod is 3

 

Calculating Ncr and Npr

Given the value of N and r. Calculate the Ncr (Combinations of N things taken r at a time) and Npr(Permutations of N things taken r at a time).
Examples: 
 

Input : N = 10, r = 3   Output : Ncr = 120  Input : N = 10, r = 3   Output : Npr = 720

The traditional implementations of Ncr and Npr are discussed here and here
Below is the discussed Python3.8 solution.
 

Python




import math
N = 10
r = 3
 
print(f"Ncr = {math.comb(N, r)}")
print(f"Npr = {math.perm(N, r)}")
 
 

Output: 
 

Ncr = 120 Npr = 720

 



Next Article
Getting Started with Competitive Programming in Python

H

Harish Vemula
Improve
Article Tags :
  • DSA
  • Mathematical
  • Python
  • python-basics
  • Python-Built-in-functions
Practice Tags :
  • Mathematical
  • python

Similar Reads

  • Python Input Methods for Competitive Programming
    Python is an amazingly user-friendly language with the only flaw of being slow. In comparison to C, C++, and Java, it is quite slower. In online coding platforms, if the C/C++ limit provided is x. Usually, in Java time provided is 2x, and in Python, it's 5x. To improve the speed of code execution fo
    6 min read
  • Which Python Modules are useful for competitive programming?
    In the previous article, we have discussed that C++, Java and Python are three most common languages for competitive programming. In this article, we are going to focus on the most important Python modules from competitive programming and interview preparation point of view. list : Dynamic Sized Arr
    3 min read
  • Getting Started with Competitive Programming in Python
    Python is a great option for programming in Competitive Programming. First off, its easy-to-understand and concise grammar enables quicker development and simpler debugging. The huge standard library of Python offers a wide range of modules and functions that can be used to effectively address progr
    11 min read
  • Must do Math for Competitive Programming
    Competitive Programming (CP) doesn’t typically require one to know high-level calculus or some rocket science. But there are some concepts and tricks which are sufficient most of the time. You can definitely start competitive coding without any mathematical background, but maths becomes essential as
    15+ min read
  • How to become a master in competitive programming?
    There are many people for whom programming is like a haunted dream. Programming is nothing but an art of talking with machines and telling them what to do, when to do, and why to do. Most of the students hear this word in high school. For many of them programming starts with ‘C’ and ends at ‘C’. The
    4 min read
  • String Guide for Competitive Programming
    Strings are a sequence of characters, and are one of the most fundamental data structures in Competitive Programming. String problems are very common in competitive programming contests, and can range from simple to very challenging. In this article we are going to discuss about most frequent string
    15 min read
  • Mathematics for Competitive Programming Course By GeeksforGeeks
    Mathematics forms the foundation of problem-solving in Competitive Programming (CP). Mastering key mathematical concepts is crucial for approaching algorithmic challenges effectively. If you're an aspiring competitive programmer or someone who wishes to enhance your problem-solving skills, this Math
    3 min read
  • Some useful C++ tricks for beginners in Competitive Programming
    Here are some of the basic C++ tricks that every beginner in Competitive Programming should follow for increased speed. However, competitive programming can only be mastered with time and lots of practice. These tricks will just help you to save a little time during the contests which sometimes matt
    3 min read
  • 5 Best Languages for Competitive Programming
    Needless to say, Competitive Programming is one of the most crucial and popular aspects of a programmer's journey. Though, all the programmers are strongly recommended to participate in such coding challenges to enhance their coding skills and to get various ravishing prizes, rewards, and other care
    5 min read
  • Which C++ libraries are useful for competitive programming?
    C++ is one of the most recommended languages in competitive programming (please refer our previous article for the reason) C++ STL contains lots of containers which are useful for different purposes. In this article, we are going to focus on the most important containers from competitive programming
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences