- 12.9k
- 2
- 71
- 55
By the very nature of this solution, its best-golfed form rounds arbitrarily – sometimes up, sometimes down. As such, it's more complicated to explain why it works, so I suggest reading the explanation of the 10499 byte always-round-down solution below first. (This is oddly appropriate, given the order of operations in regexes that use right-to-left evaluated variable-length lookbehind.)
By the very nature of this solution, its best-golfed form rounds arbitrarily – sometimes up, sometimes down. As such, it's more complicated to explain why it works, so I suggest reading the explanation of the 104 byte always-round-down solution below first. (This is oddly appropriate, given the order of operations in regexes that use right-to-left evaluated variable-length lookbehind.)
By the very nature of this solution, its best-golfed form rounds arbitrarily – sometimes up, sometimes down. As such, it's more complicated to explain why it works, so I suggest reading the explanation of the 99 byte always-round-down solution below first. (This is oddly appropriate, given the order of operations in regexes that use right-to-left evaluated variable-length lookbehind.)
- 12.9k
- 2
- 71
- 55
(?=(x*)(1円x?))(x*)(?^=((?=(2円$|))x(?^=((?^=(7円?x))(^10円|3円))*5円(x*)(?^3=9円(x*))))*1円(?^7=3円)|^|$)
# tail = N = input number
(?=
(x*) # 1円 = floor(N / 2)
(1円x?) # 2円 = ceil(N / 2)
)
(x*) # 3円 = guess for floor(sqrt(N*N/2))
(?^= # Atomic lookinto: tail = N
(
(?=(2円$|)) # 5円 = 2円 if tail == 2,円 0 otherwise;
# Note that if tail==2,円 it means this is the last iteration
# if N is odd – but if N is even, this iteration will fail to
# match, as the last iteration will have already finished.
x # tail -= 1
(?^= # Atomic lookinto: tail = N
# if 10円 is set: tail -= 10円; 7円 += 1;
# X = floor((tail - 5円) / 3円); tail -= 3円*X; 7円 += X;
# Note that in practice, 0 ≤ X ≤ 1
(
(?^=
( # 7円 = sum of the following:
7円? # previous value of 7円 if set, 0 otherwise
x # 1
)
)
(
^10円 # On the first iteration, if 10円 is set, tail -= 10円
|
3円 # Assert tail ≥ 3円; tail -= 3円
)
)* # Match the above as many times as possible, minimum 0
5円 # tail -= 5円
(x*) # 9円 = tail = remainder = dividend % 3円
(?^3= # Atomic lookinto: tail = 3円
9円 # tail -= 9円
(x*) # 10円 = tail = 3円 - 9円
)
)
)*
1円 # Assert tail ≥ 1,円 forcing the loop above to iterate 2円 times
(?^7= # Lookinto: tail = 7円
3円 # Assert 3円 ≤ tail, i.e. 3円 ≤ 7円
)
|
^$ # or N == 0, in which case the output = the input; this needs to be
# a special case because "(?^7=...)" cannot match if 7円 is unset.
)
(?=(x*)(1円x?))(x*)(?^=((?=(2円$|))x(?^=((?^=(7円?x))(^10円|3円))*5円(x*)(?^3=9円(x*))))*1円(?^7=3円)|^)
# tail = N = input number
(?=
(x*) # 1円 = floor(N / 2)
(1円x?) # 2円 = ceil(N / 2)
)
(x*) # 3円 = guess for floor(sqrt(N*N/2))
(?^= # Atomic lookinto: tail = N
(
(?=(2円$|)) # 5円 = 2円 if tail == 2,円 0 otherwise;
# Note that if tail==2,円 it means this is the last iteration
# if N is odd – but if N is even, this iteration will fail to
# match, as the last iteration will have already finished.
x # tail -= 1
(?^= # Atomic lookinto: tail = N
# if 10円 is set: tail -= 10円; 7円 += 1;
# X = floor((tail - 5円) / 3円); tail -= 3円*X; 7円 += X;
# Note that in practice, 0 ≤ X ≤ 1
(
(?^=
( # 7円 = sum of the following:
7円? # previous value of 7円 if set, 0 otherwise
x # 1
)
)
(
^10円 # On the first iteration, if 10円 is set, tail -= 10円
|
3円 # Assert tail ≥ 3円; tail -= 3円
)
)* # Match the above as many times as possible, minimum 0
5円 # tail -= 5円
(x*) # 9円 = tail = remainder = dividend % 3円
(?^3= # Atomic lookinto: tail = 3円
9円 # tail -= 9円
(x*) # 10円 = tail = 3円 - 9円
)
)
)*
1円 # Assert tail ≥ 1,円 forcing the loop above to iterate 2円 times
(?^7= # Lookinto: tail = 7円
3円 # Assert 3円 ≤ tail, i.e. 3円 ≤ 7円
)
|
^ # or N == 0, in which case the output = the input; this needs to be
# a special case because "(?^7=...)" cannot match if 7円 is unset.
)
(?=(x*)(1円x?))(x*)(?^=((?=(2円$|))x(?^=((?^=(7円?x))(^10円|3円))*5円(x*)(?^3=9円(x*))))*1円(?^7=3円)|$)
# tail = N = input number
(?=
(x*) # 1円 = floor(N / 2)
(1円x?) # 2円 = ceil(N / 2)
)
(x*) # 3円 = guess for floor(sqrt(N*N/2))
(?^= # Atomic lookinto: tail = N
(
(?=(2円$|)) # 5円 = 2円 if tail == 2,円 0 otherwise;
# Note that if tail==2,円 it means this is the last iteration
# if N is odd – but if N is even, this iteration will fail to
# match, as the last iteration will have already finished.
x # tail -= 1
(?^= # Atomic lookinto: tail = N
# if 10円 is set: tail -= 10円; 7円 += 1;
# X = floor((tail - 5円) / 3円); tail -= 3円*X; 7円 += X;
# Note that in practice, 0 ≤ X ≤ 1
(
(?^=
( # 7円 = sum of the following:
7円? # previous value of 7円 if set, 0 otherwise
x # 1
)
)
(
^10円 # On the first iteration, if 10円 is set, tail -= 10円
|
3円 # Assert tail ≥ 3円; tail -= 3円
)
)* # Match the above as many times as possible, minimum 0
5円 # tail -= 5円
(x*) # 9円 = tail = remainder = dividend % 3円
(?^3= # Atomic lookinto: tail = 3円
9円 # tail -= 9円
(x*) # 10円 = tail = 3円 - 9円
)
)
)*
1円 # Assert tail ≥ 1,円 forcing the loop above to iterate 2円 times
(?^7= # Lookinto: tail = 7円
3円 # Assert 3円 ≤ tail, i.e. 3円 ≤ 7円
)
|
$ # or N == 0, in which case the output = the input; this needs to be
# a special case because "(?^7=...)" cannot match if 7円 is unset.
)
- 12.9k
- 2
- 71
- 55
# tail = N = input number
(?=
(x*) # 1円 = floor(N / 2)
(1円x?) # 2円 = ceil(N / 2)
)
(x*) # 3円 = guess for floor(sqrt(N*N/2))
(?^= # Atomic lookinto: tail = N
(
(?=(2円$|)) # 5円 = 2円 if tail == 2,円 0 otherwise;
# Note that if tail==2,円 it means this is the last iteration
# if N is odd – but if N is even, this iteration will fail to
# match, as the last iteration will have already finished.
x # tail -= 1
(?^= # Atomic lookinto: tail = N
# if 10円 is set: tail -= 10円; 7円 += 1;
# X = floor((tail - 5円) / 3円); tail -= 3円*X; 7円 += X;
# Note that in practice, 0 ≤ X ≤ 1
(
(?^=
( # 7円 = sum of the following:
7円? # previous value of 7円 if set, 0 otherwise
x # 1
)
)
(
^10円 # On the first iteration, if 10円 is set, tail -= 10円
|
3円 # Assert tail ≥ 3円; tail -= 3円
)
)* # Match the above as many times as possible, minimum 0
5円 # tail -= 5円
(x*) # 9円 = tail = remainder = dividend % 3円
(?^3= # Atomic lookinto: tail = 3円
9円 # tail -= 9円
(x*) # 10円 = tail = 3円 - 9円
)
)
)*
1円 # Assert tail ≥ 1,円 forcing the loop above to iterate 2円 times
(?^7= # Lookinto: tail = 7円
3円 # Assert 3円 ≤ tail, i.e. 3円 ≤ 7円
)
|
^ # or N == 0, in which case the output = the input; this needs to be
# a special case because "(?^7=...)" cannot match if 7円 is unset.
)
# tail = N = input number
(?=
(x*) # 1円 = floor(N / 2)
(1円x?) # 2円 = ceil(N / 2)
)
(x*) # 3円 = guess for floor(sqrt(N*N/2))
(?^= # Atomic lookinto: tail = N
(
(?=(2円$|)) # 5円 = 2円 if tail == 2,円 0 otherwise;
# Note that if tail==2,円 it means this is the last iteration
# if N is odd – but if N is even, this iteration will fail to
# match, as the last iteration will have already finished.
x # tail -= 1
(?^= # Atomic lookinto: tail = N
# if 10円 is set: tail -= 10円; 7円 += 1;
# X = floor(tail / 3円); tail -= 3円*X; 7円 += X;
# Note that in practice, 0 ≤ X ≤ 1
(
(?^=
( # 7円 = sum of the following:
7円? # previous value of 7円 if set, 0 otherwise
x # 1
)
)
(
^10円 # On the first iteration, if 10円 is set, tail -= 10円
|
3円 # Assert tail ≥ 3円; tail -= 3円
)
)* # Match the above as many times as possible, minimum 0
5円 # tail -= 5円
(x*) # 9円 = tail = remainder = dividend % 3円
(?^3= # Atomic lookinto: tail = 3円
9円 # tail -= 9円
(x*) # 10円 = tail = 3円 - 9円
)
)
)*
1円 # Assert tail ≥ 1,円 forcing the loop above to iterate 2円 times
(?^7= # Lookinto: tail = 7円
3円 # Assert 3円 ≤ tail, i.e. 3円 ≤ 7円
)
|
^ # or N == 0, in which case the output = the input; this needs to be
# a special case because "(?^7=...)" cannot match if 7円 is unset.
)
# tail = N = input number
(?=
(x*) # 1円 = floor(N / 2)
(1円x?) # 2円 = ceil(N / 2)
)
(x*) # 3円 = guess for floor(sqrt(N*N/2))
(?^= # Atomic lookinto: tail = N
(
(?=(2円$|)) # 5円 = 2円 if tail == 2,円 0 otherwise;
# Note that if tail==2,円 it means this is the last iteration
# if N is odd – but if N is even, this iteration will fail to
# match, as the last iteration will have already finished.
x # tail -= 1
(?^= # Atomic lookinto: tail = N
# if 10円 is set: tail -= 10円; 7円 += 1;
# X = floor((tail - 5円) / 3円); tail -= 3円*X; 7円 += X;
# Note that in practice, 0 ≤ X ≤ 1
(
(?^=
( # 7円 = sum of the following:
7円? # previous value of 7円 if set, 0 otherwise
x # 1
)
)
(
^10円 # On the first iteration, if 10円 is set, tail -= 10円
|
3円 # Assert tail ≥ 3円; tail -= 3円
)
)* # Match the above as many times as possible, minimum 0
5円 # tail -= 5円
(x*) # 9円 = tail = remainder = dividend % 3円
(?^3= # Atomic lookinto: tail = 3円
9円 # tail -= 9円
(x*) # 10円 = tail = 3円 - 9円
)
)
)*
1円 # Assert tail ≥ 1,円 forcing the loop above to iterate 2円 times
(?^7= # Lookinto: tail = 7円
3円 # Assert 3円 ≤ tail, i.e. 3円 ≤ 7円
)
|
^ # or N == 0, in which case the output = the input; this needs to be
# a special case because "(?^7=...)" cannot match if 7円 is unset.
)
- 12.9k
- 2
- 71
- 55
- 12.9k
- 2
- 71
- 55
- 12.9k
- 2
- 71
- 55
- 12.9k
- 2
- 71
- 55
- 12.9k
- 2
- 71
- 55
- 12.9k
- 2
- 71
- 55
- 12.9k
- 2
- 71
- 55
- 12.9k
- 2
- 71
- 55