Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,143,503 members, 7,781,535 topics. Date: Friday, 29 March 2024 at 04:25 PM

Simple Factorial Challenge - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Simple Factorial Challenge (4205 Views)

Factorial Design Of Experiments (d.o.e) (2) (3) (4)

(1) (2) (Reply) (Go Down)

Re: Simple Factorial Challenge by olyjosh(m): 2:48pm On May 27, 2016
jacob05:
@Slyr0x
An Attempt cool

from decimal import *
import math

x = 0.7893

beginH = Decimal(1.0 )
beginB = Decimal(1.0 )

multi = 3
adder = 2

r = Decimal(0)
for i in range( 2, 5001 ) :
r = r + Decimal( ( beginH/beginB ) * Decimal( x ** i ) )

beginH = multi * beginH + adder
beginB = multi * beginB
multi +=1
adder +=1
print(r)

I begin to love dis python of a thing oo.
Your solution is quite brilliant n fast but your answer only converges upto d 15th decimal values. Unnecessary rounding in d system I guess.

1 Like

Re: Simple Factorial Challenge by jacob05(m): 7:19pm On May 27, 2016
olyjosh:


I begin to love dis python of a thing oo.
Your solution is quite brilliant n fast but your answer only converges upto d 15th decimal values. Unnecessary rounding in d system I guess.
Hmmm. Thanks
Re: Simple Factorial Challenge by slysoft: 6:46pm On May 31, 2016
jacob05:
cc: Yemoll

I think your solution or your friend's isn't quite it...

Firstly, using a factorial finding function to solve this problem is wrong and naive. e.g I believe $yyy increases linearly with loop and its factorial gets calculated as the $ii increase to 5000.. try finding the factorial of 5000.. without //bcscale(25);

Lastly, bcscale(25) ; , makes the solution very likely to be inaccurate as the result of all bc functions are trimmed or scaled to 25. wink

I'm not saying mine is correct or is not tongue ... grin grin grin ... You judge... I'm just making my point.. cool


Hi @jacob05,

I designed the php script @slyrox uploaded for the problem... as @Lyphiard rightly mentioned that the problem @OP posted came from a competition in USA which we solved in our team before sharing it nairaland to develop other programmers on this platform.

The accepted solution/result on the competition portal was "5.08845978368001251890" which is the same as the result of our script/algorithm and different from the solution/result of your script/algorithm "5.088459783680012467179075225".

However, this is how we solved it or how we got the algorithm;

Looking at the equation posted;
x^2 + (5/3)x^3 + (23/12)x^4 + (119/60)x^5 ... + (2*[5000!-1]/[5000!])x^5000
it can easily be translated into a summation equation
which is to sum the result of ((2 * ([a+1]! – 1) / [a+1]! ) * x^(a+1) from a = 1 till 5000 where x is a constant
so for a = 1 we have x^2
a = 2 we have (5/3)x^3
sum the results till a = 5000

All we did was to translate the deduced summation equation into a script/algorithm. there was no reason behind using php, I could have used several other lang java, C#, VB, C, ruby, python

The result of the problem was also requested in the nearest 20 decimal places. why i added bcscale(25) is to control the number of trailing digits after the decimal and not affect the integrity of the 20th digit after the decimal. then i took off the trailing 5 digits after the 20th decimal

I will be willing to learn if you comeup with a much quicker or less-lame solution with regards to the posted problem. Thanks smiley

BTW, My understanding of your script is that 5000 = 5000!; correct me if i am wrong.

Re: Simple Factorial Challenge by jacob05(m): 5:07am On Jun 01, 2016
slysoft:



Hi @jacob05,

I designed the php script @slyrox uploaded for the problem... as @Lyphiard rightly mentioned that the problem @OP posted came from a competition in USA which we solved in our team before sharing it nairaland to develop other programmers on this platform.

The accepted solution/result on the competition portal was "5.08845978368001251890" which is the same as the result of our script/algorithm and different from the solution/result of your script/algorithm "5.088459783680012467179075225".

However, this is how we solved it or how we got the algorithm;

1
Looking at the equation posted;
x^2 + (5/3)x^3 + (23/12)x^4 + (119/60)x^5 ... + (2*[5000!-1]/[5000!])x^5000
it can easily be translated into a summation equation
which is to sum the result of ((2 * ([a+1]! – 1) / [a+1]! ) * x^(a+1) from a = 1 till 5000 where x is a constant
so for a = 1 we have x^2
a = 2 we have (5/3)x^3
sum the results till a = 5000


All we did was to translate the deduced summation equation into a script/algorithm. there was no reason behind using php, I could have used several other lang java, C#, VB, C, ruby, python

The result of the problem was also requested in the nearest 20 decimal places. why i added bcscale(25) is to control the number of trailing digits after the decimal and not affect the integrity of the 20th digit after the decimal. then i took off the trailing 5 digits after the 20th decimal

2: I will be willing to learn if you comeup with a much quicker or less-lame solution with regards to the posted problem. Thanks smiley


BTW, My understanding of your script is that 35000 = 5000!; correct me if i am wrong.


And that is still naive and the wrong way of solving the problem (Although it gives you the correct answer ). The organizers of code competitions knows the easy, and mostly expensive, way of solving the problem and they most times set that trap for you. (Learnt the Hard way in the Google Code Jam 2016).
Always avoid factorial loops that move towards 1000 ... It's a trap !!!... Finding factorial is an expensive runtime operation.(Especially one that tends towards 1000) Quote me if I'm wrong.

2:

from decimal import *
import math

x = Decimal('0.7893')

beginH = Decimal('1.0' )
beginB = Decimal('1.0' )

multi = Decimal('3' )
adder = Decimal('2' )

r = Decimal('0.0')
for i in range( 2, 5001 ) :
r = r + ( ( beginH/beginB ) * Decimal( x ** i ) )

beginH = multi * beginH + adder
beginB = multi * beginB
multi +=1
adder +=1
print '{0:.21g}'.format(r)

Funny enough, It's relatively the same script I posted!!!. The issue with previous was with my use of float for x.. (floats are mostly not accurate)
and this
beginH = Decimal(1.0).

The issue with the above is that 1.0 is a floating point value .. which is not exactly 1.0 when converted into decimal...(don't blame me.. tongue tongue grin grin ). Just had to put single quotes... beginH = Decimal('1.0')... Did the same for the rest... lobatan... grin

3. Yes but I did not use the expensive factorial method to calculate the result. smiley
Re: Simple Factorial Challenge by slysoft: 11:16am On Jun 01, 2016
@jacob05

Impressive script, i just learned something.

The loop in your script started from 2 till 5001... can you explain why?
Re: Simple Factorial Challenge by jacob05(m): 11:23am On Jun 01, 2016
slysoft:
@jacob05

Impressive script, i just learned something.

The loop in your script started from 2 till 5001... can you explain why?

x^2 + (5/3)x^3 + (23/12)x^4 + (119/60)x^5 ... + (2*[5000!-1]/[5000!])x^5000

The power starts from 2 to 5000... range( 2, 5001 ) generates a sequence from 2 to 5001 - 1 (5000).. wink
Re: Simple Factorial Challenge by jacob05(m): 11:25am On Jun 01, 2016
xrange would be better though... performance wise. wink ... But It won't kill for now.. grin grin grin

(1) (2) (Reply)

Vb vs Java: Which Is Better For Mobile Applications? / Were Can I Download Free Video Tutorials On Javascript Or Ruby / Can I Call Myself A Good Programmer If I Only Know Visual Basic

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 62
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.