Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,628 members, 7,801,816 topics. Date: Friday, 19 April 2024 at 12:19 AM

Simple Factorial Challenge - Programming - Nairaland

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

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

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

Simple Factorial Challenge by Slyr0x: 10:26pm On May 15, 2016
Bola discovered a mysterious piece of paper, containing a mysterious formula:

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


Bola was curious, and so decided to evaluate the expression for x=0.7893

What number was returned, to the nearest 20 decimal places?
Re: Simple Factorial Challenge by Slyr0x: 3:30pm On May 17, 2016
bump
Re: Simple Factorial Challenge by larisoft: 8:27am On May 18, 2016
@OP... your questions are real brain heaters. I love them! But would you please provide the solutions after each of them has been unsolved for hours?. Many of us attempt them for hours and it will be nice to finally know the solution after trying so hard!
Re: Simple Factorial Challenge by Nobody: 9:07am On May 18, 2016
DIS LAND IS 4 SALE NOT.



here is my incomplete solution.

Re: Simple Factorial Challenge by Nobody: 2:54pm On May 18, 2016
Nice one crotonite



(0.7893)^(500)×(0.7893)^(10)
= ( 0.7893)^5000

(5000!-1)÷(5000!) is estimated as 0.99 something removing one has no much effect .


damm infinitedecimal , I can't get the exact value manually .
I didn't solve for the all the sum to the 499 th term .
only the first to the 5th series was solved with high level of manipulation .
Re: Simple Factorial Challenge by kosytedy(m): 3:26pm On May 18, 2016
crotonite:
DIS LAND IS 4 SALE NOT.



here is my incomplete solution.

@crotonite, your code contains some errors, I can't start typing all of them. But the fact() function is wrong as it should contain a loop.

Factorial of 4 (4!) is = 4 x 3 x 2 x 1 = 24

Programmatically, the loop will have to start right at 5000 (value passed into the function) and decrease by one with each decrement multiplied by the already gotten result till it stops looping, then the final answer is given. Please review your codes again. Nice try, keep it up.

1 Like

Re: Simple Factorial Challenge by Slyr0x: 3:34pm On May 18, 2016
larisoft:
@OP... your questions are real brain heaters. I love them! But would you please provide the solutions after each of them has been unsolved for hours?. Many of us attempt them for hours and it will be nice to finally know the solution after trying so hard!

I sure will.

Thanks
Re: Simple Factorial Challenge by jacob05(m): 4:15pm On May 18, 2016
@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)
Re: Simple Factorial Challenge by abumeinben(m): 5:23pm On May 18, 2016
crotonite:
DIS LAND IS 4 SALE NOT.



here is my incomplete solution.

Me wan sabi this .java
Re: Simple Factorial Challenge by Nobody: 8:13pm On May 18, 2016
Slyr0x:


I sure will.

Thanks
I have an answer with proxy20 infinite decimal equation . My aim was to solve it partially .
Re: Simple Factorial Challenge by jacob05(m): 8:16pm On May 18, 2016
proxy20:

I have an answer with proxy20 infinite decimal equations . May aim was to solve it partially .
Hmmm. Interesting. grin

1 Like

Re: Simple Factorial Challenge by Nobody: 8:18pm On May 18, 2016
jacob05:

Hmmm. Interesting. grin
cool
Re: Simple Factorial Challenge by Lyphiard: 2:40am On May 19, 2016
Please do not answer this problem. The OP has taken this question from a competition and is attempting to cheat.
Re: Simple Factorial Challenge by mojeed4(m): 8:24am On May 20, 2016
kosytedy:


@crotonite, your code contains some errors, I can't start typing all of them. But the fact() function is wrong as it should contain a loop.

Factorial of 4 (4!) is = 4 x 3 x 2 x 1 = 24

Programmatically, the loop will have to start right at 5000 (value passed into the function) and decrease by one with each decrement multiplied by the already gotten result till it stops looping, then the final answer is given. Please review your codes again. Nice try, keep it up.


Lol. I guess you have not heard of a recursion method. Try running the fact() function and you will see that it will run perfectly well. Although using a for loop will also do the job, but I believe the recursion method does the job finer and cleaner. (My opinion though).
Re: Simple Factorial Challenge by Slyr0x: 3:43pm On May 20, 2016
Lyphiard:
Please do not answer this problem. The OP has taken this question from a competition and is attempting to cheat.

Calm your tits.

I solved the challenges before posting here, and the answer for this is 5.08xxxxxxxxxxxxxxxxxx.

I will release the write up later smiley.

Cheers Kiddo,

R.
Re: Simple Factorial Challenge by jacob05(m): 4:34pm On May 20, 2016
Slyr0x:


Calm your tits.

I solved the challenges before posting here, and the answer for this is 5.08xxxxxxxxxxxxxxxxxx.

I will release the write up later smiley.

Cheers Kiddo,

R.

Guess my solution is correct then. cool
Re: Simple Factorial Challenge by Nobody: 9:26pm On May 20, 2016
mojeed4:



Lol. I guess you have not heard of a recursion method. Try running the fact() function and you will see that it will run perfectly well. Although using a for loop will also do the job, but I believe the recursion method does the job finer and cleaner. (My opinion though).

grin i saw his comment and i was like WTF!

1 Like

Re: Simple Factorial Challenge by thedummy: 8:05am On May 21, 2016
Slyr0x:


Calm your tits.

I solved the challenges before posting here, and the answer for this is 5.08xxxxxxxxxxxxxxxxxx.

I will release the write up later smiley.

Cheers Kiddo,

R.

Hey Slyr0x, how did you do it I've been reading this thread and I tried to solve this for almost 6-8 hours.
Please Share
Re: Simple Factorial Challenge by olyjosh(m): 7:40am On May 23, 2016
Please guys, do submit your computation time and memory usage for these solutions. Anybody could have solve it, but let's see how effective is your solution in term of space and time.
You can log ur codes here https://ideone.com/ or on your machine(please do state the machine processor and memory configuration).
Thanks
Re: Simple Factorial Challenge by jacob05(m): 8:03am On May 23, 2016
olyjosh:
Please guys, do submit your computation time and memory usage for these solutions. Anybody could have solve it, but let's see how effective is your solution in term of space and time.
You can log ur codes here https://ideone.com/ or on your machine(please do state the machine processor and memory configuration).
Thanks

https://ideone.com/DSusUY
Re: Simple Factorial Challenge by olyjosh(m): 8:29am On May 23, 2016
jacob05:


https://ideone.com/DSusUY
Nice one Boss.
Your solution runs for 0.22s 11640KB
Re: Simple Factorial Challenge by Slyr0x: 11:51am On May 23, 2016
Great job Jacob05. Your solution is more efficient.

Solution below as solved by someone on my team

bcscale(25);
$itotal = "0";
$xt = "0.7893";$ii = 1;
$yyy = "1";while ($ii <= 5000)
{
$y2 = intval(gmp_add($yyy,"1"wink);
$y3 = strval($y2);

$factit = gmp_fact($y2);

$at = bcdiv(gmp_mul("2", gmp_sub($factit,"1"wink), $factit);
$xtpow = bcpow($xt,$y2);

$anst = bcmul($at,$xtpow);

$itotal = bcadd($itotal,$anst);
$yyy = gmp_add($yyy,"1"wink;
$ii++;

}

echo "Solution: " .$itotal."<br />";
Re: Simple Factorial Challenge by jacob05(m): 11:56am On May 23, 2016
Slyr0x:
Great job Jacob05. Your solution is more efficient.

Solution below as solved by someone on my team

bcscale(25);
$itotal = "0";
$xt = "0.7893";$ii = 1;
$yyy = "1";while ($ii <= 5000)
{
$y2 = intval(gmp_add($yyy,"1"wink);
$y3 = strval($y2);

$factit = gmp_fact($y2);

$at = bcdiv(gmp_mul("2", gmp_sub($factit,"1"wink), $factit);
$xtpow = bcpow($xt,$y2);

$anst = bcmul($at,$xtpow);

$itotal = bcadd($itotal,$anst);
$yyy = gmp_add($yyy,"1"wink;
$ii++;

}

echo "Solution: " .$itotal."<br />";

Thanks
Re: Simple Factorial Challenge by Yemoll: 2:42pm On May 23, 2016
Slyr0x:
Great job Jacob05. Your solution is more efficient.

Solution below as solved by someone on my team

bcscale(25);
$itotal = "0";
$xt = "0.7893";$ii = 1;
$yyy = "1";while ($ii <= 5000)
{
$y2 = intval(gmp_add($yyy,"1"wink);
$y3 = strval($y2);

$factit = gmp_fact($y2);

$at = bcdiv(gmp_mul("2", gmp_sub($factit,"1"wink), $factit);
$xtpow = bcpow($xt,$y2);

$anst = bcmul($at,$xtpow);

$itotal = bcadd($itotal,$anst);
$yyy = gmp_add($yyy,"1"wink;
$ii++;

}

echo "Solution: " .$itotal."<br />";

@slyrox if you run both scripts @jacob05's python and the php you uploaded, they are not providing the same output. So there is something wrong somewhere.
Re: Simple Factorial Challenge by Slyr0x: 2:51pm On May 23, 2016
^^I will check that in a bit.

Updated


Lol, Jacob05 , you've got the wrong answer. I ran your code, saw the first 5.088459******* and then assumed you got it right.
Re: Simple Factorial Challenge by jacob05(m): 3:25pm On May 23, 2016
cc: Yemoll
Slyr0x:
^^I will check that in a bit.

Updated


Lol, Jacob05 , you've got the wrong answer. I ran your code, saw the first 5.088459******* and then assumed you got it right.

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
Re: Simple Factorial Challenge by olyjosh(m): 8:02pm On May 23, 2016
I tried not to post this solution because it breaks the 5secs limit on ideone but I have gat no other improvement on it for now



import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

/**
*
* @author Olyjosh
*/
public class SimpleFactorial {

private static BigDecimal sum = new BigDecimal("0"wink;
private static final BigDecimal x=new BigDecimal(0.7893);
private static final BigDecimal TWO=new BigDecimal(2);
private static BigDecimal exp = x;
private static BigInteger bi=BigInteger.ONE;


private static BigDecimal factBig(int n){
BigDecimal bd;
BigDecimal div;
for (int i = 2; i <= n; i++) {
bi=bi.multiply(new BigInteger(""+i));
//BigInteger factBig = factBig();
bd = new BigDecimal(bi);
div = bd.subtract(BigDecimal.ONE)
.divide(bd, 21, RoundingMode.HALF_UP);
exp=exp.multiply(x);
sum=sum.add(div.multiply(exp).multiply(TWO));
}
return sum.setScale(20, RoundingMode.HALF_UP);
}


public static void main(String[] args) {
System.out.println(factBig(5000));
}
}



Ans = 5.08845978368001258389

However this run for about 1825millisec on my 2.5 GHz Intel Core i5
Re: Simple Factorial Challenge by dozies1: 5:39pm On May 26, 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)
here is my solution:
. 
from math import factorial
from decimal import Decimal

n = 5000
x = Decimal('0.7893')
y = Decimal('0')

for i in range(1, n+1):
y = y + 2 * (x**i) * (factorial(i)-1) / factorial(i)
print y

Please, would you mind telling me why mine is much more slower than yours.
Re: Simple Factorial Challenge by jacob05(m): 6:07pm On May 26, 2016
dozies1:

here is my solution:
. 
from math import factorial
from decimal import Decimal

n = 5000
x = Decimal('0.7893')
y = Decimal('0')

for i in range(1, n+1):
y = y + 2 * (x**i) * (factorial(i)-1) / factorial(i)
print y

Please, would you mind telling me why mine is much more slower than yours.
Because you're finding and summing the factorial of numbers from 1 to 5000 : which is quite computation intensive.
Re: Simple Factorial Challenge by dozies1: 12:37am On May 27, 2016
jacob05:

Because you're finding and summing the factorial of numbers from 1 to 5000 : which is quite computation intensive.
Ok.
What is the principle behind making it go faster.
Re: Simple Factorial Challenge by jacob05(m): 7:20am On May 27, 2016
dozies1:
Ok.
What is the principle behind making it go faster.
Don't use factorial.. Simple
Re: Simple Factorial Challenge by dozies1: 9:33am On May 27, 2016
jacob05:
Don't use factorial.. Simple
OK. Thank yo

(1) (2) (Reply)

Vb vs Java: Which Is Better For Mobile Applications? / Were Can I Download Free Video Tutorials On Javascript Or Ruby / Where Are The Good Coders?

(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. 51
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.