Back to General discussions forum
Hello Admin... :)
I've tried to solve a problem "Greatest Common Divisor" on PHP and by my opinion I've done it because my version of solution works on my local server (I've only presented input data as an array) and it returns correct values (I've comparead them with your answers twice).
As I've noticed the problem appears during getting input data but the method that I've used for getting input data worked perfectly for earlier problems.
So I can't understand what to do.
Can you help me please?
Hi,
I know little about PHP. (Admin probably knows more, but he hasn't been around for a long time)
The first line of the input contains the number of test cases. It would be easier to help you if your code read that value, and then processed that number of test cases.
I tried your code with the example data and it worked (as long as there wasn't a blank line at the end).
When I tried it with a few more test cases I got
Please wait upto 20 seconds
then that was replaced with
No syntax errors detected in solution.php
I think it's doing that because your code is slow as it is calculating the gcd using subtraction.
So, apart from reading the number of test cases in the first line, you could try
a) use modulo instead of subtraction
or
b) running it on your local system, then copy/paste the answer into the "Your answer" field
Thank you Quandray...
Modulo really helped... It was mentioned about modulo in description of the problem but I did not consider that it would be so important... :)
Good luck...
Hello, I have a little tip for you all:
When you are reading the information about the problem, you can find the next phrase:
"For speeding up the process we can use modulo operation instead of subtraction."
You can apply this suggestion, but do it carefully. If you find the "1" inside the loops, the modulo will be "0", so, in the next cycle, you will crash with a beautiful "Divided by zero" exception.
In summary, combine the modulo with subtraction, so your code will run wihthout issues.
Thank you for your attention.