Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

Haskell, 48 bytes

f n=1+6*sum[(mod(i+1)3-1)*div(n^2)i|i<-[1..n^2]]

Try it online!

Uses xnor's "black magic" formula:

$$f(n)=1+6\sum_{a=0}^\infty \left\lfloor \frac{n^2}{3a+1}\right\rfloor - \left\lfloor \frac{n^2}{3a+2}\right\rfloor$$

A proof of its correctness, and an explanation of how xnor managed to express it in 43 bytes of Python, can be found here.

Long story short: we count Eisenstein integers of norm \1ドル \le N \le n^2\$, by factoring \$N = (x+y\omega)(x+y\omega^*)\$ into Eisenstein primes and counting how many solutions for \$(x,y)\$ come out of the factorization. We recognize the number of solutions as being equal to

$6ドル \times ((\text{# of divisors of }N \equiv 1\space(\text{mod }3)) - (\text{# of divisors of }N \equiv 2\space(\text{mod }3)))$$

and apply a clever trick to make that really easy to compute for all integers between \1ドル\$ and \$n^2\$ at once. This yields the formula above. Finally, we apply some Python golf magic to end up with the really tiny solution xnor found.

Haskell, 48 bytes

f n=1+6*sum[(mod(i+1)3-1)*div(n^2)i|i<-[1..n^2]]

Try it online!

Uses xnor's "black magic" formula:

$$f(n)=1+6\sum_{a=0}^\infty \left\lfloor \frac{n^2}{3a+1}\right\rfloor - \left\lfloor \frac{n^2}{3a+2}\right\rfloor$$

A proof of its correctness, and an explanation of how xnor managed to express it in 43 bytes of Python, can be found here.

Long story short: we count Eisenstein integers of norm \1ドル \le N \le n^2\$, by factoring \$N = (x+y\omega)(x+y\omega^*)\$ into Eisenstein primes and counting how many solutions for \$(x,y)\$ come out of the factorization. We recognize the number of solutions as being equal to

$6ドル \times ((\text{# of divisors of }N \equiv 1\space(\text{mod }3)) - (\text{# of divisors of }N \equiv 2\space(\text{mod }3)))$$

and apply a clever trick to make that really easy to compute for all integers between \1ドル\$ and \$n^2\$ at once. This yields the formula above. Finally, we apply some Python golf magic to end up with the really tiny solution xnor found.

Haskell, 48 bytes

f n=1+6*sum[(mod(i+1)3-1)*div(n^2)i|i<-[1..n^2]]

Try it online!

Uses xnor's "black magic" formula:

$$f(n)=1+6\sum_{a=0}^\infty \left\lfloor \frac{n^2}{3a+1}\right\rfloor - \left\lfloor \frac{n^2}{3a+2}\right\rfloor$$

A proof of its correctness, and an explanation of how xnor managed to express it in 43 bytes of Python, can be found here.

Long story short: we count Eisenstein integers of norm \1ドル \le N \le n^2\$, by factoring \$N = (x+y\omega)(x+y\omega^*)\$ into Eisenstein primes and counting how many solutions for \$(x,y)\$ come out of the factorization. We recognize the number of solutions as being equal to

$6ドル \times ((\text{# of divisors of }N \equiv 1\space(\text{mod }3)) - (\text{# of divisors of }N \equiv 2\space(\text{mod }3)))$$

and apply a clever trick to make that really easy to compute for all integers between \1ドル\$ and \$n^2\$ at once. This yields the formula above. Finally, we apply some Python golf magic to end up with the really tiny solution xnor found.

Formatted explanation using MathJax
Source Link
Mr. Xcoder
  • 42.9k
  • 9
  • 87
  • 221

Haskell, 48 bytes

f n=1+6*sum[(mod(i+1)3-1)*div(n^2)i|i<-[1..n^2]]

Try it online!

Uses xnor's "black magic" formula:

xnor's formula $$f(n)=1+6\sum_{a=0}^\infty \left\lfloor \frac{n^2}{3a+1}\right\rfloor - \left\lfloor \frac{n^2}{3a+2}\right\rfloor$$

A proof of its correctness, and an explanation of how xnor managed to express it in 43 bytes of Python, can be found here.

Long story short: we count Eisenstein integers of norm 1 ≤ N ≤ n2\1ドル \le N \le n^2\$, by factoring N = (x+yω)(x+yω*)\$N = (x+y\omega)(x+y\omega^*)\$ into Eisenstein primes and counting how many solutions for (x,y)\$(x,y)\$ come out of the factorization. We recognize the number of solutions as being equal to

6 ×ばつ ((# of divisors of N ≡ 1 mod 3) − (# of divisors of N ≡ 2 mod 3)),$6ドル \times ((\text{# of divisors of }N \equiv 1\space(\text{mod }3)) - (\text{# of divisors of }N \equiv 2\space(\text{mod }3)))$$

and apply a clever trick to make that really easy to compute for all integers between 1\1ドル\$ and n2\$n^2\$ at once. This yields the formula above. Finally, we apply some Python golf magic to end up with the really tiny solution xnor found.

Haskell, 48 bytes

f n=1+6*sum[(mod(i+1)3-1)*div(n^2)i|i<-[1..n^2]]

Try it online!

Uses xnor's "black magic" formula:

xnor's formula

A proof of its correctness, and an explanation of how xnor managed to express it in 43 bytes of Python, can be found here.

Long story short: we count Eisenstein integers of norm 1 ≤ N ≤ n2, by factoring N = (x+yω)(x+yω*) into Eisenstein primes and counting how many solutions for (x,y) come out of the factorization. We recognize the number of solutions as being equal to

6 ×ばつ ((# of divisors of N ≡ 1 mod 3) − (# of divisors of N ≡ 2 mod 3)),

and apply a clever trick to make that really easy to compute for all integers between 1 and n2 at once. This yields the formula above. Finally, we apply some Python golf magic to end up with the really tiny solution xnor found.

Haskell, 48 bytes

f n=1+6*sum[(mod(i+1)3-1)*div(n^2)i|i<-[1..n^2]]

Try it online!

Uses xnor's "black magic" formula:

$$f(n)=1+6\sum_{a=0}^\infty \left\lfloor \frac{n^2}{3a+1}\right\rfloor - \left\lfloor \frac{n^2}{3a+2}\right\rfloor$$

A proof of its correctness, and an explanation of how xnor managed to express it in 43 bytes of Python, can be found here.

Long story short: we count Eisenstein integers of norm \1ドル \le N \le n^2\$, by factoring \$N = (x+y\omega)(x+y\omega^*)\$ into Eisenstein primes and counting how many solutions for \$(x,y)\$ come out of the factorization. We recognize the number of solutions as being equal to

$6ドル \times ((\text{# of divisors of }N \equiv 1\space(\text{mod }3)) - (\text{# of divisors of }N \equiv 2\space(\text{mod }3)))$$

and apply a clever trick to make that really easy to compute for all integers between \1ドル\$ and \$n^2\$ at once. This yields the formula above. Finally, we apply some Python golf magic to end up with the really tiny solution xnor found.

Bounty Awarded with 500 reputation awarded by xnor
Source Link
lynn
  • 69.7k
  • 11
  • 137
  • 285

Haskell, 48 bytes

f n=1+6*sum[(mod(i+1)3-1)*div(n^2)i|i<-[1..n^2]]

Try it online!

Uses xnor's "black magic" formula:

xnor's formula

A proof of its correctness, and an explanation of how xnor managed to express it in 43 bytes of Python, can be found here.

Long story short: we count Eisenstein integers of norm 1 ≤ N ≤ n2, by factoring N = (x+yω)(x+yω*) into Eisenstein primes and counting how many solutions for (x,y) come out of the factorization. We recognize the number of solutions as being equal to

6 ×ばつ ((# of divisors of N ≡ 1 mod 3) − (# of divisors of N ≡ 2 mod 3)),

and apply a clever trick to make that really easy to compute for all integers between 1 and n2 at once. This yields the formula above. Finally, we apply some Python golf magic to end up with the really tiny solution xnor found.

AltStyle によって変換されたページ (->オリジナル) /