Handy Functions

From Grundy
Jump to navigation Jump to search

This is just a page with a list of functions that may come handy to people, mostly in C++.

Reverse a Number

1 int reverse_number(int n)
2 {
3     int result = 0;
4     do{
5         result = result*10+n%10;
6         n = n/10;
7     }while(n>0);
8     return result;
9 }

Occurences of a Prime in a Factorial

1 int occurences_of_prime_in_factorial(int N,int p){
2     int result = 0;
3     while(N/p){
4         result += N/p;
5         N /= p;
6     }
7     return result;
8 }

Euclid's GCD of two numbers

1 int gcd(int a, int b){
2     return (a==0)? b : gcd(b%a, a);
3 }