# Tag Archives: number-theory

## Finding sum of digits of a number until sum becomes single digit

Given a number n, we need to find the sum of its digits such that: If n < 10 digSum(n) = n Else digSum(n) =… Read More »

## How to prepare for ACM – ICPC?

ACM ICPC(Association for Computing Machinery – International Collegiate Programming Contest) is a world-wide annual multi-tiered programming contest being organized for over thirteen years. The contest… Read More »

## Carmichael Numbers

A number n is said to be a Carmichael number if it satisfies the following modular arithmetic condition: power(b, n-1) MOD n = 1, for… Read More »

## Pollard’s Rho Algorithm for Prime Factorization

Given a positive integer n, and that it is composite, find a divisor of it. Example: Input: n = 12; Output: 2 [OR 3 OR… Read More »

## Compute n! under modulo p

Given a large number n and a prime p, how to efficiently compute n! % p? Examples : Input: n = 5, p = 13… Read More »

## Top 10 Algorithms and Data Structures for Competitive Programming

In this post “Important top 10 algorithms and data structures for competitive coding “. Topics : Graph algorithms Dynamic programming Searching and Sorting: Number theory… Read More »

## Primality Test | Set 3 (Miller–Rabin)

Given a number n, check if it is prime or not. We have introduced and discussed School and Fermat methods for primality testing. Primality Test… Read More »

## Primality Test | Set 2 (Fermat Method)

Given a number n, check if it is prime or not. We have introduced and discussed School method for primality testing in Set 1. Primality… Read More »

## Primality Test | Set 1 (Introduction and School Method)

Given a positive integer, check if the number is prime or not. A prime is a natural number greater than 1 that has no positive… Read More »

## Euler’s Totient function for all numbers smaller than or equal to n

Euler’s Totient function Φ(n) for an input n is count of numbers in {1, 2, 3, …, n} that are relatively prime to n, i.e.,… Read More »

## Check if a number can be expressed as x^y (x raised to power y)

Given a positive integer n, find if it can be expressed as xy where y > 1 and x > 0. x and y both… Read More »

## Puzzle | Program to find number of squares in a chessboard

Puzzle: You are provided with a chessboard and are asked to find the number of squares in it. A chessboard is a board with 8… Read More »

## Modular multiplicative inverse

Given two integers ‘a’ and ‘m’, find modular multiplicative inverse of ‘a’ under modulo ‘m’. The modular multiplicative inverse is an integer ‘x’ such that.… Read More »

## Euler’s Totient Function

Euler’s Totient function ?(n) for an input n is count of numbers in {1, 2, 3, …, n} that are relatively prime to n, i.e.,… Read More »

## Legendre’s formula (Given p and n, find the largest x such that p^x divides n!)

Given an integer n and a prime number p, find the largest x such that px (p raised to power x) divides n! (factorial) Examples:… Read More »