ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 03 Sep 2020 07:23:29 +0200How to handle very large numbershttps://ask.sagemath.org/question/53274/how-to-handle-very-large-numbers/I want to play around with factorials. I found that `factorial(10**9)` takes about 20 minutes (on my machine, which is a Mac mini with 8GB Memory). Then I tried `factorial(10**10)` and there I got this:
/Applications/...pathtoSageMath/sage/src/bin/sage-python: line 2: 975 Killed: 9 sage - python "$@"
Then I searched for a SageMath Stirling Formula, which I didn't find (only some related stuff), so I tried it myself:
sage: def s(n):
....: x=sqrt(2*pi*n)*((n/e)**n)
....: return x.numerical_approx()
....:
sage: s(5)
118.019167957590
sage: s(6)
710.078184642185
sage: s(10**10)
This gave me an error after some minutes:
RuntimeError
...
4047 relational_operator(self._gobj))
4048 else:
-> 4049 x = g_pow(self._gobj, nexp._gobj)
4050 return new_Expression_from_GEx(self._parent, x)
4051
RuntimeError: size of exponent exceeds signed long size
Then I tried s(10**9), there I got another error:
python3(755,0x10fbecdc0) malloc: can't allocate region
:*** mach_vm_map(size=18446744073151754240, flags: 100) failed (error code=3)
python3(755,0x10fbecdc0) malloc: *** set a breakpoint in malloc_error_break to debug
sig_error() without sig_on()
------------------------------------------------------------------------
(no backtrace available)
------------------------------------------------------------------------
Unhandled SIGABRT: An abort() occurred.
This probably occurred because a *compiled* module has a bug
in it and is not properly wrapped with sig_on(), sig_off().
Python will now terminate.
So what can I do? Is there a special library for very large reals or int or some special commands for getting an approximation of how many decimals a factorial will have? Could I use the `log` somehow or would this have the same runtime problems?
The reason why I'm asking is that I am rewriting a text which is 20 years old and there's a statement that `factorial(10**100)` could not be computed (ok, I believe that because of factorial being between `O(2**n)` and `O(2**(2**n))`) and `factorial(10**8)` would take many hours, which was not the case on my machine (see above).
And last but not least I would like to know if there is an upper bound for numbers that sage can handle. I thought not, but now I know otherwise ;-)
Also hints to good literature on these topics would be appreciated.
dantetanteThu, 03 Sep 2020 07:23:29 +0200https://ask.sagemath.org/question/53274/Can Sage simplify factorials?https://ask.sagemath.org/question/39369/can-sage-simplify-factorials/I am starting out with Sage and am playing around with some simple equations involving factorials. I have not been able to find a way for Sage to simplify the equations. Does anyone know how, if possible, this is done? The set up is:
sage: var("x, y, z")
(x, y, z)
sage: eq1 = x^2 - y^2 == 0
sage: solve(eq1, x, y)
([x == -y, x == y], [1, 1])
sage: eq2 = factorial(x) - factorial(y) == 0
sage: solve(eq2, x, y)
([factorial(x) == factorial(y)], [1])
sage: eq2.simplify()
factorial(x) - factorial(y) == 0
sage: eq2.simplify_factorial()
factorial(x) - factorial(y) == 0
sage: eq2.simplify_full()
factorial(x) - factorial(y) == 0
As you can see sage can handle polynomials in eq1 but does not seem to recognise that x == y is a solution for eq2. I have tried adding some assume commands in to restrict to positive integers but this does not help. I have not been able to find anything in the documentation so far about this seemingly simple situation which must occur frequently so if anyone has any suggestions please let me know.burrobertThu, 02 Nov 2017 04:19:30 +0100https://ask.sagemath.org/question/39369/How do I evaluate sum() containing factorial()?https://ask.sagemath.org/question/9937/how-do-i-evaluate-sum-containing-factorial/I am trying to evaluate a sum containing a factorial, but need to do copy and paste of the interim result to get the final answer in the sage notebook (ver. 5.7). Is there a direct way?
var('i k n t')
sum(factorial(3-i)*k^i*t^i, i,0,n)(k=1, n=3, t=4)
> sum(4^i*factorial(-i + 3), i, 0, 3)
If I copy the result into a new input cell and evaluate:
sum(4^i*factorial(-i + 3), i, 0, 3)
only then I obtain the desired
>94
This is annoying, as I would like to compute the result for a long list of n and t and plot the results.
EDIT:
Maybe I simplifed the question too much. Just to specify again why I would like to use symbolics: I actually wanted to evaluate
var('i k n t')
sum(factorial(n-i)*k^i*t^i, i,0,n)
for different values of n and get the symbolic result, e.g. for n = 3, I would expect:
3*k^3*t^3 + 2*k^2*t^2 + k*t
**EDIT2:** Betrema's edited solution is very helpful:
[sum(factorial(n-i)*k^i*t^i, i, 0, n) for n in range(3)]
gives
[1, k*t + 1, k^2*t^2 + k*t + 2]
as desired. **The only remaining question is:
Why does sum(factorial(n-i)*k^i*t^i, i, 0, n)(n=3)
not give k^2*t^2 + k*t + 2?**
Does the .subs() method work differently on symbolic sums than on other symbolic equations?
Thanks again!
stanFri, 22 Mar 2013 17:15:40 +0100https://ask.sagemath.org/question/9937/Exact factorial/gamma values for half-integershttps://ask.sagemath.org/question/8877/exact-factorialgamma-values-for-half-integers/is it possible to return exact values of (n+1/2)! and ?(n+1/2) for *n* is an integer?
[Particular Values of the Gamma Function](http://en.wikipedia.org/wiki/Particular_values_of_the_Gamma_function)daniel.e2718Thu, 10 May 2012 20:43:01 +0200https://ask.sagemath.org/question/8877/Inequalities, solving problemshttps://ask.sagemath.org/question/8723/inequalities-solving-problems/1.)
2!·4!·...·20!
______________
1!·3!·...·19!
(this is a fraction)
I need to solve this in a closed form. the question is that we need to find out if it is better to solve in its original form or we need to 'help it a little'?
Now I typed down the whole exercise.
The other problem:
2./a)
1+ 1/2 + 1/3 +...+ 1/n >10
We need positive n that works for this.
2./b)
We need the smallest n. (not only the answer, we need to prove why that's the answer)
I wrote down all the questions for the exercises. The whole thing is in a sage document. We can upload only sage files to the server too. The teacher is tricky by the way. The subject is called Solving mathematics problems with sage. Sadly I cannot upload pictures because I'd need more karma.
I can try to do this in the analytical way, and type it in sage, but any help or tips would be great with the proof too.. :) I have no idea what my teacher wants really.
pepeMon, 27 Feb 2012 12:10:41 +0100https://ask.sagemath.org/question/8723/Factorial simplification errorhttps://ask.sagemath.org/question/8187/factorial-simplification-error/I've run into an issue when running simplify on an equation containing a factorial:
sage: var('n')
sage: z1 = (-1)^(1/2*(n - 1))*factorial(n) == 0
sage: z2 = 0 == (-1)^(1/2*(n - 1))*factorial(n)
sage: z1.simplify()
n*(-1)^(1/2*n - 1/2) != 0
sage: z2.simplify()
0 == (-1)^(1/2*n - 1/2)*factorial(n)
My best guess is when sage sends the equation down to maxima for simplification it's interpreting
(-1)^(1/2*(n - 1))*n! = 0
as
(-1)^(1/2*(n - 1))*n != 0
Any ideas on how I can work around this?
garretThu, 23 Jun 2011 16:17:42 +0200https://ask.sagemath.org/question/8187/