Fun Facts and General Concepts

4 minute read

This page is dedicated to some of the day to day life normal math stuff. I will keep updating this page to my heart’s content. Haha !!!

I will highly recommend you to use the table of contents, shown on right side, to browse the topics. This post will be too long

Highest power of number in a factorial

An Example and Little Theory

Lets say we have a given number’s, factorial and we are asked to find the highest power of say another number . How we will do it?

For example and .

A manual testing for will give us the following result,

The next question arises is from where we will get ? We can get from all the multiples of . Hmm… so can we say the quotient of will give us what will be the highest power of ? Yes its partially correct, but have you considered these cases,

If you have missed these numbers you didn’t considered few powers of which should be counted. Thus, our next step will be to consider them too.

We are still short by twos. You will but your brain into action and see the manual calculations and finds out you haven’t considered following numbers,

Theory

From the above example we saw,

Formula 1

Above observations leads to one thing only, count how many multiples are there for each power of in given factorial , till the multiples for a particular power becomes .

Testing in our above sample example

Formula 2 : Neat trick

Well there is another very simple way of doing this and saving our-self from powers minefield.

We just have to repeatedly divide quotient by till we get as quotient. Add all the quotients, that will be our answer.

For Eg:

Code

/**
 * A function to calculate the highest power of a number "a" in N!
 *
 * @param num
 *            the factorial number
 * @param a
 *            the number whose highest power needs to be calculated
 *
 * @return the highest power of "a" in N!
 */
public static long highestPowerInFactorial(long num, long a) {
  long quotientSum = 0;

  while (num != 0) {
    num /= a;
    quotientSum += num;
  }

  return quotientSum;
}

Complexity

For a given factorial and number , complexity = . Because we are always reducing the size by in every step.

Find the maximum power of a number below a factorial

The question is find the maximum value of for a given in for which

I will just write the code. Its a simple problem.

Code

/**
 * A function to calculate the Maximum Power Of a Number In Factorial
 *
 * @param num
 *            the factorial number
 * @param a
 *            the number whose highest power needs to be calculated
 *
 * @return the maximum power of number in Factorial
 */
public static long maximumPoweredValueOfNumberInFactorial(long num, long a) {

  long maxPower = 0;
  long currentNumber = a;
  while (num > currentNumber) {
    currentNumber *= a;
    maxPower++;
  }

  return maxPower;
}

Number of zeros at the end of a factorial

How to find the number of trailing zeroes in a given factorial?

Theory

Well to make a we need one and one and that will make a trailing zero in a factorial. That’s it. How to count how many and are there ? Basically we need to know whats the highest power of and are there in . We discussed this in above subsection(Here).

Lets say,

Since we know that every alternate number is an even number so it will donate one at least and every number will be a multiple of . It implies that the power of will always be less that power of , thus simply returning the highest power of in a given factorial is enough.

Code

/**
 * A function to calculate the trailing zeroes in a factorial num
 *
 * @param num
 *            the factorial number
 *
 * @return number of trailing zeroes
 */
public static long numberOfTrailingZeroesInAFactorial(long num) {
  return highestPowerInFactorial(num, 5);
}

Complexity

For a given factorial , complexity = .

Sum of first N natural numbers

Sum of first Natural numbers

These are also called Triangular numbers

The sum of square of first terms of natural number

The sum of cube of first terms of natural number

References

Leave a Comment

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

Loading...