10
votes
Accepted
Weighted coin flip strings
05AB1E, 40 39 18 17 bytes
Brute-force approach:
0Lα1ݲãʒγOà³@}èPO
Byte-count more than halved by porting @JonathanAllan's Jelly answer, which uses a similar ...
9
votes
Implement Binary Exponentiation
Vyxal, 6 bytes
d‡½⌊İe
Try it Online!
Takes inputs in reverse order.
Explanation
...
8
votes
Print the power set of the power set ... of an empty set
Burlesque, 8 bytes
{}1qR@C~
Try it online!
...
8
votes
Accepted
Print the power set of the power set ... of an empty set
Jelly, 3 bytes
ŒP¡
Attempt This Online!
Using standard I/O rules; takes input from STDIN
Explanation:
...
7
votes
Weighted coin flip strings
Jelly, 19 15 bytes
Ø.ṗṣ1ZṫɗƇ_⁵AP€S
A full program accepting n, k, and ...
7
votes
Weighted coin flip strings
Python, 66 bytes (-1 @xnor)
f=lambda n,k,p:n>=k and(1-p*(n>k))*(1-f(n+~k,k,p))*p**k+f(n-1,k,p)
Attempt This Online!
Adapted from my answer to a similar challenge. Note that I add memoization in ...
6
votes
Print the power set of the power set ... of an empty set
Wolfram Language (Mathematica), 19 bytes
Nest[Subsets,{},#]&
Try it online!
In the notebook interface, the results are printed without spaces. But in TIO or ...
6
votes
Print the power set of the power set ... of an empty set
Vyxal, 4 bytes
or 12
¾?(ṗ
Explanation
...
6
votes
Print the power set of the power set ... of an empty set
Python, 96 bytes
from itertools import*
f=lambda n:n and(chain(*(combinations(f(n-1),r)for r in range(n+1))))or()
Attempt This Online!
5
votes
Print the power set of the power set ... of an empty set
Haskell, 53 bytes
f=(!!)$iterate(\l->init l++[','|l/="{}"]++l++"}")"{}"
Try it online!
My first golf. Nothing particularly clever,...
5
votes
Weighted coin flip strings
R, 129 122 116 bytes
Edit: -2 bytes thanks to pajonk (which led to -4 more...)
...
4
votes
Is this polynomial a square?
Vyxal, 33 32 bytes
h√d£0¾ȦλȮḂÞ•-¥/J;RI÷?₂J†^¥J:⌊=JA
Try it Online!
–1 per emanresu A.
How?
We find the square root of the polynomial using its power series, ...
4
votes
Print the power set of the power set ... of an empty set
Pyth, 3 or 17 11 bytes
yFY
Try it online!
F - Repeat Q times, where Q is the implicit input:
...
4
votes
Divide by an odd number, 2-adically
Python, 45 bytes
lambda a,b,n:(a-~a)*pow(b-~b,-1,2<<n)//2%2**n
Attempt This Online!
Beginning with Python 3.8 the builtin pow can directly compute the modular ...
4
votes
Weighted coin flip strings
JavaScript (ES7), 106 bytes
Quickly computes the probability, using the formula found by Kevin Cruijssen.
...
4
votes
Weighted coin flip strings
Vyxal ḋ, 23 bytes
2$ÞẊ'2€vLG›¹>;⁰⌐⁰"$İvΠ∑
Try it Online!
Port of Jelly. Really slow.
Previous answer (much faster):
...
4
votes
Weighted coin flip strings
PARI/GP, 53 bytes
k->p->g(n)=if(n>=k,p^k*(1-p*(n>k))*(1-g(n---k))+g(n))
Attempt This Online!
A port of loopy walt's Python answer. Takes input in the ...
3
votes
Finite Field Multiplication
Jelly, 22 bytes
Bæc/ḣ-64æcB}ʋƬUSḣ64UḂḄ
Try it online!
Works either as a function or a full program. This takes two arguments: a 2-element list ...
3
votes
3
votes
Landau logarithm
Haskell, 68 67 bytes
k%n=sum[1+k?n%n|k?n>k]
k?n=maximum$k:[lcm x$k?(n-x)|x<-[1..n]]
(1%)
Try it online!
Golfs 2 bytes ...
3
votes
Semidivisibility
Vyxal v2.6.0+, 6 bytes
ġ/₀KḢc
Takes its input as \$k\$ followed by \$n\$.
Try it Online!
This uses the same algorithm as my regex answer, which is similar to the ...
3
votes
Is this polynomial a square?
Wolfram Language (Mathematica), 58 68 64 55 bytes
thanks @Steffan for the byte correction, and the suggestions
thanks @alephalpha
...
3
votes
Print the power set of the power set ... of an empty set
05AB1E (legacy), 4 or 17 bytes
Regular I/O using [] instead of {} and spaces after the commas would be 4 bytes:
...
3
votes
Divide by an odd number, 2-adically
PARI/GP, 32 bytes
f(a,b,n)=(2*a+1)/(2*b+1)%2^n++\2
Attempt This Online!
PARI/GP has a built-in p-adic type, but using the % ...
3
votes
Divide by an odd number, 2-adically
C (gcc), 48 bytes
c;i;f(a,b){for(i=32;i;--i)c=a-b-2*b*c;return c;}
Try it online!
Explanation: since \$(2b+1) (2c+1) = 2(2bc+b+c)+1\$ should end up equal to \$2a+...
3
votes
3
votes
Weighted coin flip strings
PARI-GP, 75 bytes
Port of below solution (can run n ~ 1000000000, k ~ 150)
f(n,k,p)=(matrix(k++,k,a,b,(a-1==b)*p+(a<2)*(b<k)*(1-p)+(a+b==k+k))^n)[k,1]
Python ...
3
votes
Implement Binary Exponentiation
PARI/GP, 34 bytes
f(x,n)=[x^m\=2|i<-binary(m=2*n+1)]
Attempt This Online!
PARI/GP, 29 bytes
f(a,n)=if(n,f(a,n\2)*x+a^n,1)
...
3
votes
3
votes
Implement Binary Exponentiation
Jelly, 5 bytes
:Ƭ2*@
Try it online!
Takes n on the left, and x on the right. Based on ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
math × 1646code-golf × 1482
number × 282
sequence × 207
arithmetic × 179
number-theory × 160
geometry × 108
integer × 86
primes × 85
combinatorics × 81
decision-problem × 79
code-challenge × 74
array × 65
matrix × 62
string × 55
polynomials × 50
rational-numbers × 48
fastest-code × 44
abstract-algebra × 38
ascii-art × 36
base-conversion × 34
linear-algebra × 31
restricted-source × 28
open-ended-function × 28
fibonacci × 27