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

Fix byte count (in reference to other version) that was accidentally not updated
Source Link
Deadcode
  • 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.)

fix PCRE+lookinto round-down version to work after bug-fix in RegexMathEngine
Source Link
Deadcode
  • 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.
)
update a comment in the PCRE+lookinto version in light of the 113->96 byte golf
Source Link
Deadcode
  • 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.
)
-1 byte on PCRE+lookinto round-down version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
oops, I missed a spot in standardizing the comments
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
use the nomenclature for the language header I established later than this original post, for indicating RegexMathEngine
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-4 bytes on PCRE+lookinto round-down version; port comment changes to other versions
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
update .NET round-down explanation
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
fix comments in PCRE+lookinto round-down version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-13 bytes on PCRE+lookinto version by porting the .NET golf of combining the two counters
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-5 bytes on .NET round-down version; fix typos in PCRE+lookinto round-down text
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
fix a comment in the lookinto versions
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-1 bytes on PCRE+lookinto version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-4 bytes on PCRE+lookinto version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-1 byte on PCRE+lookinto version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
-4 bytes on PCRE+lookinto version
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
add PCRE+lookinto ports of both versions, with replit.com RegexMathEngine test harnesses
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
added 263 characters in body
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Show the byte sizes of the first-working (pre-post) version of each regex
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
amend the speculation about less-powerful-than-.NET but more-powerful-than-ECMAScript flavors
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Make note of why variable-length lookbehind is needed by this algorithm
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading
Source Link
Deadcode
  • 12.9k
  • 2
  • 71
  • 55
Loading

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