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.Fri, 10 Feb 2023 16:29:06 +0100Binary partition functionhttps://ask.sagemath.org/question/66270/binary-partition-function/Hello, I am trying to figure out how to return the number of partitions of n as powers of 2 (sometimes called the binary partition function $b(n)$). For example, $4 = 2^2 = 2^1+2^0+2^0 = 2^0+2^0+2^0+2^0$, then $b(4)=3$.
I just can't get anything to work. I would really appreciate some help. Thank you so much! This community is always so helpful!
If the code can be written so that it gives the number of partitions of $n$ as powers of $k$ that would be unbelievably helpful.Tue, 07 Feb 2023 14:13:34 +0100https://ask.sagemath.org/question/66270/binary-partition-function/Answer by Max Alekseyev for <p>Hello, I am trying to figure out how to return the number of partitions of n as powers of 2 (sometimes called the binary partition function $b(n)$). For example, $4 = 2^2 = 2^1+2^0+2^0 = 2^0+2^0+2^0+2^0$, then $b(4)=3$. </p>
<p>I just can't get anything to work. I would really appreciate some help. Thank you so much! This community is always so helpful!</p>
<p>If the code can be written so that it gives the number of partitions of $n$ as powers of $k$ that would be unbelievably helpful.</p>
https://ask.sagemath.org/question/66270/binary-partition-function/?answer=66271#post-id-66271This should do the job for given `n` and `k`:
def b(n, k=2):
return Partitions(n,parts_in=[k**i for i in range(n.ndigits(k))]).cardinality()
Btw, you missed $4 = 2^1 + 2^1$ and so $b(4)=4$.Tue, 07 Feb 2023 15:55:49 +0100https://ask.sagemath.org/question/66270/binary-partition-function/?answer=66271#post-id-66271Comment by Max Alekseyev for <p>This should do the job for given <code>n</code> and <code>k</code>:</p>
<pre><code>def b(n, k=2):
return Partitions(n,parts_in=[k**i for i in range(n.ndigits(k))]).cardinality()
</code></pre>
<p>Btw, you missed $4 = 2^1 + 2^1$ and so $b(4)=4$.</p>
https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66333#post-id-66333You mixed up things. `.cardinality()` method is defined for `Partitions(...)` object. If you want to use it, you'd need to provide `parts_in=` parameter to it. Such as `Partitions(n, parts_in=divisors(n)).cardinality()`.Fri, 10 Feb 2023 16:29:06 +0100https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66333#post-id-66333Comment by brennan for <p>This should do the job for given <code>n</code> and <code>k</code>:</p>
<pre><code>def b(n, k=2):
return Partitions(n,parts_in=[k**i for i in range(n.ndigits(k))]).cardinality()
</code></pre>
<p>Btw, you missed $4 = 2^1 + 2^1$ and so $b(4)=4$.</p>
https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66332#post-id-66332Excellent, thank you so much! And that is perfect, I am starting to see how this works. I am having some trouble with one more function. I am looking to find the partitions of $n$ among the divisors of $n$. I tried adapting some of the code above. I used:
def d(n): return sum(number_of_partitions(k) for k in divisors(n))
which returned the sum of the divisors. I tried omitting sum and I tried replacing it with len but neither worked. I then tried
def d(n): return (number_of_partitions(k) for k in divisors(n)).cardinality()
But again to no avail. I am not sure what I am doing wrong, I thought that the divisors(n) command was a list of the divisors, not a sum?
Again, thank you so much for all the help!Fri, 10 Feb 2023 15:53:43 +0100https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66332#post-id-66332Comment by Max Alekseyev for <p>This should do the job for given <code>n</code> and <code>k</code>:</p>
<pre><code>def b(n, k=2):
return Partitions(n,parts_in=[k**i for i in range(n.ndigits(k))]).cardinality()
</code></pre>
<p>Btw, you missed $4 = 2^1 + 2^1$ and so $b(4)=4$.</p>
https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66317#post-id-66317Alternative formula for https://oeis.org/A025065:
def a025065(n):
return sum( number_of_partitions(k) for k in range(n//2+1) )
PS. Code formatting is achieved by adding 4 spaces at the beginning of each line. There is also a button for code formatting at the top of edit window, which depicts 0's and 1's.Thu, 09 Feb 2023 19:40:44 +0100https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66317#post-id-66317Comment by achrzesz for <p>This should do the job for given <code>n</code> and <code>k</code>:</p>
<pre><code>def b(n, k=2):
return Partitions(n,parts_in=[k**i for i in range(n.ndigits(k))]).cardinality()
</code></pre>
<p>Btw, you missed $4 = 2^1 + 2^1$ and so $b(4)=4$.</p>
https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66287#post-id-66287A naive attempt of obtaining some numbers from https://oeis.org/A000837 :
[len([x for x in Partitions(n) if gcd(x)==1]) for n in range(2,20)]
[1, 2, 3, 6, 7, 14, 17, 27, 34, 55, 63, 100, 119, 167, 209, 296, 347, 489]
For palindromes:
def pal(n):
return Partitions(n,parts_in=[1]+srange(2,n+1,2)).cardinality()Wed, 08 Feb 2023 17:28:13 +0100https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66287#post-id-66287Comment by brennan for <p>This should do the job for given <code>n</code> and <code>k</code>:</p>
<pre><code>def b(n, k=2):
return Partitions(n,parts_in=[k**i for i in range(n.ndigits(k))]).cardinality()
</code></pre>
<p>Btw, you missed $4 = 2^1 + 2^1$ and so $b(4)=4$.</p>
https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66311#post-id-66311This is perfect. Again thank you so much. I am trying now to get the terms of OEIS A025065, partitions into palindromes. There is a comment there that this sequence is equivalent to partitions among the set $\{1,2,4,6,8,\ldots\}$ so I tried the following:
\ \ \
E=Integers(1,2n)
def Pal(n):
return len([x for x in Partitions(n) if x in E])
\ \ \
But that isn't giving me what I want. I am trying to get a handle on this, and everyone has been so helpful.
Also, why is my code not rendering is sage format? I am following the style guide https://ask.sagemath.org/question/51344/code-and-markdown-cell/Thu, 09 Feb 2023 12:17:11 +0100https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66311#post-id-66311Comment by Max Alekseyev for <p>This should do the job for given <code>n</code> and <code>k</code>:</p>
<pre><code>def b(n, k=2):
return Partitions(n,parts_in=[k**i for i in range(n.ndigits(k))]).cardinality()
</code></pre>
<p>Btw, you missed $4 = 2^1 + 2^1$ and so $b(4)=4$.</p>
https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66286#post-id-66286Converting `fibonacci_sequence(1, n+1)` into a list is an overkill. More efficient is to use module `itertools`:
import itertools
fibs = itertools.takewhile(lambda x: x<=n, fibonacci_sequence(1, n+1))
Alternatively, one can compute the largest index of Fiboinacci number via logarithm of $n\sqrt{5}$ base the golden ratio.Wed, 08 Feb 2023 14:36:09 +0100https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66286#post-id-66286Comment by achrzesz for <p>This should do the job for given <code>n</code> and <code>k</code>:</p>
<pre><code>def b(n, k=2):
return Partitions(n,parts_in=[k**i for i in range(n.ndigits(k))]).cardinality()
</code></pre>
<p>Btw, you missed $4 = 2^1 + 2^1$ and so $b(4)=4$.</p>
https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66279#post-id-66279Using @Max Alekseyev remarks below:
def b(n):
import itertools
fibs = itertools.takewhile(lambda x: x<=n,fibonacci_sequence(2, n+1))
return Partitions(n,parts_in=fibs).cardinality()
[`b(x) for x in range(4,21)]`
[4, 6, 8, 10, 14, 17, 22, 27, 33, 41, 49, 59, 71, 83, 99, 115, 134]
compare https://oeis.org/A003107Wed, 08 Feb 2023 08:55:00 +0100https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66279#post-id-66279Comment by brennan for <p>This should do the job for given <code>n</code> and <code>k</code>:</p>
<pre><code>def b(n, k=2):
return Partitions(n,parts_in=[k**i for i in range(n.ndigits(k))]).cardinality()
</code></pre>
<p>Btw, you missed $4 = 2^1 + 2^1$ and so $b(4)=4$.</p>
https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66285#post-id-66285This is perfect, thank you so much! I cannot tell you how helpful this is! I have one more if that is ok. I am trying to find the aperiodic partition function, partitions of $n$ into relatively prime parts. OEIS A000837. I don't even know where to start here.Wed, 08 Feb 2023 13:53:05 +0100https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66285#post-id-66285Comment by brennan for <p>This should do the job for given <code>n</code> and <code>k</code>:</p>
<pre><code>def b(n, k=2):
return Partitions(n,parts_in=[k**i for i in range(n.ndigits(k))]).cardinality()
</code></pre>
<p>Btw, you missed $4 = 2^1 + 2^1$ and so $b(4)=4$.</p>
https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66276#post-id-66276Thank you so much! And yes, I missed that, good catch!
Do you think it would be possible to modify this code slightly to return partitions of $n$ among the Fibonacci numbers?
I tried using:
def f(n):
return partitions(n,parts_in=[fibonacci(n) for i in range(n.ndigits(k))]).cardinality()
But that didn't work. I tried some variations as well but I am getting the error that "Error, Integer Operations: <divisor> bust be nonzero"
Sorry if I am asking too much, I really appreciate your help!Wed, 08 Feb 2023 06:04:57 +0100https://ask.sagemath.org/question/66270/binary-partition-function/?comment=66276#post-id-66276