[sage-support] Re: Does the digits method have an inverse?

2008-10-22 Thread John Cremona

2008/10/22 Timothy Clemans [EMAIL PROTECTED]:

 def from_digits(lis):
 return ZZ(''.join([str(i) for i in lis[::-1]]))


Or even

sage: n = 150
sage: dig = n.digits()
sage: PolynomialRing(ZZ,'x')(dig)(2)
150

but I agree that this should be a provided function.

NB trac ticket #2796 may soon change the default base in digits from 2 to 10.

John Cermona

 On Wed, Oct 22, 2008 at 12:35 AM, Jason Merrill [EMAIL PROTECTED] wrote:

 sage: 1492.digits(10)
 [2, 9, 4, 1]

 Now is there an easy way to take this list and get back the integer
 1492?

 Regards,

 JM
 


 


--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~--~~~~--~~--~--~---



[sage-support] Re: Does the digits method have an inverse?

2008-10-21 Thread Mike Hansen

On Tue, Oct 21, 2008 at 9:35 PM, Jason Merrill [EMAIL PROTECTED] wrote:

 sage: 1492.digits(10)
 [2, 9, 4, 1]

 Now is there an easy way to take this list and get back the integer
 1492?

I'm not sure if there is a single function that does it, but you can
use the following one liner which does what you think you should do to
reconstruct the number from its digits:

sage: n = 10; sum([d*n**i for i,d in enumerate(1492.digits(n))])
1492
sage: n = 10; sum([d*n**i for i,d in enumerate(123.digits(n))])
123
sage: n = 3; sum([d*n**i for i,d in enumerate(123.digits(n))])
123

--Mike

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~--~~~~--~~--~--~---



[sage-support] Re: Does the digits method have an inverse?

2008-10-21 Thread Timothy Clemans

def from_digits(lis):
 return ZZ(''.join([str(i) for i in lis[::-1]]))

On Wed, Oct 22, 2008 at 12:35 AM, Jason Merrill [EMAIL PROTECTED] wrote:

 sage: 1492.digits(10)
 [2, 9, 4, 1]

 Now is there an easy way to take this list and get back the integer
 1492?

 Regards,

 JM
 


--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~--~~~~--~~--~--~---