Many programming problems (especially on Brilliant) often ask one to determine if certain numbers possess unique qualities. Knowing how to implement this in the code is often a challenge and these algorithms come up frequently when dealing with harder programming problems. Try writing the most efficient method to solve the following 10 problems:
- Determine the sum of the digits/product of the digits of a particular number
- Determine whether a given number is a square number/cube number
- Determine whether a given number has unique digits
- Determine whether the digits of a number are in ascending/descending order
- Determine whether a given number is prime or composite
- Determine the prime factorisation for a given number
- Determine the number of factors a particular number has
- Determine the nth Fibonacci number
- Determine the LCM and GCD of two or more given numbers
- Reverse the digits of a particular number
By solving these problems, you develop an "arsenal" of useful algorithms.
Another problem I discussed with Sudeep Salgia:
i) Calculate the nth root of a number
POST THE BEST SOLUTIONS
#ComputerScience
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
to ensure proper formatting.2 \times 3
2^{34}
a_{i-1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
Here are my solutions in Java. Most of the things are coded by me but some parts are taken from StackOverflow (which are more efficient than my code) -
1) Sum of digits (number is stored as String) -
Product of digits (number is stored as String) -
2) Determine whether a given number is a square number -
Determine whether a given number is a cube number -
3) Determine whether a given number has unique digits (number is stored as String)
4) Check if digits are in ascending order -
Check if digits are in descending order -
5) Prime / Composite test -
6) Prime Factorization -
7) Number of factors -
8) Nth Fibonacci Number -
9) GCD -
LCM -
10) Reverse digits (number is stored as string)-
Nth root -
Additionally, I think that having a generalized Josephus problem solver at our disposal won't be bad. Here is my code. I have tested it but it might still be a bit buggy (known bug - it doesn't work if in josephus(m,n) n>m. I'll fix it later) -
Log in to reply
Thanks for the response! Very helpful!
Log in to reply
You are welcome :)
For the (6) Prime Factorization : What's max?
Log in to reply
Problem 6
This is the simplest solution that I could come up with for finding the prime factorisation of smaller numbers (Python 3.4.1 Solution):
By max, I mean number. The code was from my last year's programming assignment and I forgot to rename the max variable to number before posting it here. Thanks for pointing it out.
To determine the sum of digits of a particular number.
Start
Declare integer n , where n is the number whose sum of digits is to be found.
Input n,
Initialize variable s=0, where s will store the sum of digits.
For n>0, repeat this procedure
s=s+[n(mod10)] (% operator in C++)
n=⌊10n⌋
Print s
Stop
Log in to reply
This will only work, iff n is a non-negative integer. But besides negative numbers, there are also real numbers, for instance 44.65.
Ironically, most of the competitive programming problems outside brilliant.org is not "mathematical".
Log in to reply
Not necessarily . Have you checked out Project Euler ?
Log in to reply
Yes, but compare with codeforces, SPOJ, UVa and other informatics olympiad, they are the minority.
Log in to reply
The algorithm returns positive value if number is in desc negative if number is in asc and 0 if number is neither.
To find whether the number is a perfect square by counting the number of factors. (C++ solution)
It makes use of the property that a perfect square has odd number of factors.
Log in to reply
The time efficiency of your solution is O(n), which makes it not suitable for large numbers. The better approach is to see if the square root of the number is an integer.
Pseudocode
Log in to reply
How is time efficiency determined? What is O(n) ?
Log in to reply
Log in to reply
O(n), but mine is constant, that is the run time doesn't increase as the number gets larger, so time efficiency is O(1).
Basically, time efficiency is determined by seeing how run time increased as your variable gets larger. In this case, it's increasing linearly, henceLog in to reply
O(1) but imprecise and so your code won't work for large n. If it is computed precisely (say, integer square root to avoid handling with infinite decimal digits), it depends on implementation (this is one of the questions asked above); one implementation that I can think of is binary searching, and it takes O(logn⋅M), where M is the time taken to multiply two logn-digit numbers.
I can't help but comment on this, even though it has been a long time. If square root is computed to a floating-point format, it isMoral: When dealing with large numbers, arithmetic operations can no longer be assumed to be constant time.
@Brock Brown You're welcome!
Log in to reply
I have a doubt regarding a variant of the 4th question
How would you proceed if there were n terms(in no specificorder) and you were asked to arrange them in asc or desc order ?
Log in to reply
We will use a sorting algorithm to sort the given number's digits in ascending/descending order.
Log in to reply
P.S. Have you ever check this out ? Kishlaya gave me this link today .
Log in to reply
Log in to reply
Log in to reply
Here
Log in to reply
Do contribute more ! Even I am planning to start on with wikis on Front End and DBMS .
@Azhaghu Roopesh M The answer is pretty easy, we do not need to use any kind of sorting algorithm. Here is the algorithm:
For ascending order.
Problem 1 (Python 3.4.1 Solution)
The logic used in this algorithm is very useful. It would be a good idea to keep it in your memory bank.
Similarly:
Problem 2 (Python 3.4.1 Solution)
Does anybody know a way of determining if a number is a cube number, a power of 4, a power of 5 etc.?
Log in to reply
Try computing the nth root and finding its difference with its floor. If it is zero then it is a perfect nth power otherwise it ain't.
Log in to reply
Hi Sudeep. As always, it's always a pleasure to discuss things with you.
How do you compute the nth root of a number?
Log in to reply
In C++ the pow() function can be used like pow(a,1/n) which returns the nth root. (This is what I recall. It has been a pretty long time since I had tried this which I remember to be giving the correct output. ) I do not have any idea about Python. But if you want a platform independent algorithm without using such standard functions I would have to think about it a bit.
Log in to reply
I'll add it as an extra problem (problem 11)
Problem 3 (Python 3.4.1 Solution)
Problem 4 (Python 3.4.1 Solution)
Part 1 - Ascending Order
Log in to reply
Do you mean strictly increasing or equal or greater than previous? If it's the first one, your code will fail. Even if it's the latter, the built-in sorting algorithm for Python is Timsort that has a time efficiency of O(nlgn) and memory efficiency O(n).
In my opinion, the better approach is follow the hard way. It may looks longer, but it's more efficient. As an example below, it takes only time efficiency O(n) and memory efficiency O(1).
Pseudocode
In this way, you can either change it to strictly increasing or equal or greater than previous by using < or ≤.
Log in to reply
This is very insightful! Thanks for sharing. Also, it's adaptable code because this way you can also chose whether you want to determine if a given number is strictly increasing or equal or greater than the previous.
@Mark Mottian There is a way to find the nth root of a number without using a function. We will use logarithms.
@Sudeep Salgia
For 9, you can use Euclid's algorithm to get the gcd, then use the fact that a∗b=gcd∗lcm to get the lcm.
algorithm to calculate nth root of a number: {int n,x,y; int exponential(int x,int n); printf ("values of x & n");
}
int exponential(int x,int n) { if(n==1) return x; else return (x*exponential(x,n-1)); }
thank you for sharing.
To determine whether a given number is prime or composite. (C++ solution)
Again by counting the number of factors, making use of the property that a prime number has only 2 factors. (Sorry, @Christopher Boo )
Number 10 : Prints the INTEGER in reverse
May I question why these are algorithms everybody needs to know?