What is the largest power of 3 that divides 5 6 7 9 4 8 9 5 2 !
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Why does this work? What are you counting?
Note: In CS, explaining why your code does what you claim it does, is important. You should be able to explain the underlying structure of your code in a clean, concise way.
I've edited it now so I've explained each line with a comment.
PHP solution -
1 2 3 4 |
|
This is exactly similar to counting trailing zeroes . Here is a general formula to calculate largest power of x in n ! :
∑ i = 1 n ⌊ x i n ⌋
Simple standard approach.
Largest power α of a prime p in n ! is calculated by α = p − 1 n − s p ( n ) where s p ( n ) is the sum of the digits of n when written in base p . This can easily be proved by using the formula for the sum of a finite geometric series.
Problem Loading...
Note Loading...
Set Loading...
This is my code in Python 2.7:
This gives the output: