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

#shell oneliner (coreutils): 45chars

shell oneliner (coreutils): 45chars

Not defining a function here... just a oneliner that takes one argument in $n and scans the integer range (actually a bit more to make code shorter). The 55 character version:

seq 5e9|grep $n|factor|awk '{if(NF==2)print 2ドル}'|head -n1

It's not even too slow. For n=0123456 it returns 201234563 in 81.715s. That's impressively fast for a long pipeline with two string processors.

Removing two characters (down to 53) and one pipe, we can get it running even faster:

seq 5e9|grep $n|factor|awk '{if(NF==2){print 2ドル;exit}}'

And finally, some sed wizardry to bring it down to 45 characters, although the printout is ugly:

seq 5e9|grep $n|factor|sed -n '/: \w*$/{p;q}'

n=000 -> 10007: 10007 (user 0.017s)

n=012345 -> 10123457: 10123457 (user 7.11s)

n=0123456 -> 201234563: 201234563 (user 66.8s)

#shell oneliner (coreutils): 45chars

Not defining a function here... just a oneliner that takes one argument in $n and scans the integer range (actually a bit more to make code shorter). The 55 character version:

seq 5e9|grep $n|factor|awk '{if(NF==2)print 2ドル}'|head -n1

It's not even too slow. For n=0123456 it returns 201234563 in 81.715s. That's impressively fast for a long pipeline with two string processors.

Removing two characters (down to 53) and one pipe, we can get it running even faster:

seq 5e9|grep $n|factor|awk '{if(NF==2){print 2ドル;exit}}'

And finally, some sed wizardry to bring it down to 45 characters, although the printout is ugly:

seq 5e9|grep $n|factor|sed -n '/: \w*$/{p;q}'

n=000 -> 10007: 10007 (user 0.017s)

n=012345 -> 10123457: 10123457 (user 7.11s)

n=0123456 -> 201234563: 201234563 (user 66.8s)

shell oneliner (coreutils): 45chars

Not defining a function here... just a oneliner that takes one argument in $n and scans the integer range (actually a bit more to make code shorter). The 55 character version:

seq 5e9|grep $n|factor|awk '{if(NF==2)print 2ドル}'|head -n1

It's not even too slow. For n=0123456 it returns 201234563 in 81.715s. That's impressively fast for a long pipeline with two string processors.

Removing two characters (down to 53) and one pipe, we can get it running even faster:

seq 5e9|grep $n|factor|awk '{if(NF==2){print 2ドル;exit}}'

And finally, some sed wizardry to bring it down to 45 characters, although the printout is ugly:

seq 5e9|grep $n|factor|sed -n '/: \w*$/{p;q}'

n=000 -> 10007: 10007 (user 0.017s)

n=012345 -> 10123457: 10123457 (user 7.11s)

n=0123456 -> 201234563: 201234563 (user 66.8s)

Source Link
orion
  • 3.2k
  • 1
  • 15
  • 16

#shell oneliner (coreutils): 45chars

Not defining a function here... just a oneliner that takes one argument in $n and scans the integer range (actually a bit more to make code shorter). The 55 character version:

seq 5e9|grep $n|factor|awk '{if(NF==2)print 2ドル}'|head -n1

It's not even too slow. For n=0123456 it returns 201234563 in 81.715s. That's impressively fast for a long pipeline with two string processors.

Removing two characters (down to 53) and one pipe, we can get it running even faster:

seq 5e9|grep $n|factor|awk '{if(NF==2){print 2ドル;exit}}'

And finally, some sed wizardry to bring it down to 45 characters, although the printout is ugly:

seq 5e9|grep $n|factor|sed -n '/: \w*$/{p;q}'

n=000 -> 10007: 10007 (user 0.017s)

n=012345 -> 10123457: 10123457 (user 7.11s)

n=0123456 -> 201234563: 201234563 (user 66.8s)

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