New answers tagged integer
0
Jelly, 11 bytes
⁹Ḷ_H%⁹;Ɗ€ṢG
Try it online!
1
Perl 5, 32 bytes
An alternative to @KjetilS's solution of the same length.
say$_,$",unpack c,chr for 0..255
Try it online!
1
AWK, 48 bytes
BEGIN{for(;i<256;i++)print +i,i<128?+i:-i+2*j++}
Try it online!
Had to workaround the fact that bitwise functions in GNU AWK don't accept negative arguments. Anyway, this should work on any AWK implementation.
2
Nim -d:danger, 33 32 bytes
for b in 0..255:b.echo' ',b.int8
Try it online!
Nim, 40 39 bytes
for b in 0..255:b.echo' ',cast[int8](b)
Try it online!
Probably doesn't work on big-endian architectures.
1
Wolfram Language (Mathematica), 38 36 bytes
Print@i@Mod[i,256,-128]~Do~{i,0,255}
Try it online!
1
Scala, 46 45 bytes
()=>for(i<-0 to 255)print(f"$i,${i.toByte} ")
Try it online!
1
Retina 0.8.2, 43 bytes
256$*_¶
_
$.` -$.'¶
128`-.+
$%`
Try it online! Uses space and newline as the delimiters, plus adds a variety of trailing spaces and newlines. Explanation:
256$*_¶
Insert 256 _s plus a newline which is used to balance out the number of characters after each _.
_
$.` -$.'¶
Replace each _ with a line giving the number of preceding ...
1
Charcoal, 17 bytes
IE⁺…⁰⊗℅@…±⊗℅@⁰⟦κι
Try it online! Link is to verbose version of code. Outputs in decimal with single and double newline as separators. Explanation:
… Range from
⁰ Literal `0` to
@ Literal string `@`
℅ ASCII code i.e. 64
⊗ Doubled i.e. 128
⁺ ...
7
Bash + coreutils, 30
(seq 0 127;seq -128 -1)|nl -v0
Try it online!
1
Pyth, 17 16 bytes
V256++Nd-J127xNJ
Try it online!
-1 byte thanks to FryAmTheEggman!
Python 3.8 translation:
for N in range(256):print(str(N)+" "+str((J:=127)-N^J))
1
Rust, 45 bytes
||for i in 0..256{print!("{}:{}
",i,i as i8)}
Try it online!
Note the newline literal, output format is
1:1
2:2
...
This prints the loop count as both an i32 and an i8. Note that as casts are not allowed to panic, so they may result in unexpected behavior on overflow.
2
PowerShell, 29 33 bytes
+4 bytes: fit to the task rules. Thanks @Zaelin Goodman
0..127+-128..-1|%{"$(($i++);$_)"}
Try it online!
1
Perl 5, 32 bytes
say"$_ ",$_-2*($_&128)for 0..255
Try it online!
3
Husk, 16 bytes
†szeŀ256Sṙohṡ128
Try it online!, Another method
Explanation
†szeŀ256Sṙohṡ128
ṡ128 symmetric range of 128 [-128...128]
oh remove last element
Sṙ rotate 128 spaces
ŀ256 range [0...255]
ze zip the two, creating pairs
†s convert all the numbers to strings
...
2
PowerShell, 34 bytes
0..255|%{"$($_;($_-bxor128)-128)"}
Try it online!
2
Python 2, 49 53 51 bytes
Saved 2 bytes taking a peek at ovs's Python answer!!!
Added 8 bytes to fix a rule violation kindly pointed out by ovs.
Saved 2 bytes thanks to ovs!!!
f=lambda i=255:i*' 'and'%d '*2%(i,127-i^127)+f(i-1)
Try it online!
2
Python 2, 37 bytes
for i in range(256):print i,127-i^127
Try it online!
1
Julia 1.0, 31 bytes
Output format:
0 => 0
1 => 1...
println.(i=>i%Int8 for i=0:255)
Try it online!
4
APL (Dyalog Extended), 18 bytes
Full program printing to stdout with ¯ as negative symbol. Requires 0-based indexing.
(⍳∘≢,⍪)…127,-128…1
Try it online!
128…1 integers 128 through 1
- negate those
127, prepend 127
… fill integers from 0 until the first element of that (127)
(…) apply the tacit function:
,⍪ columnify and prepend [a column consisting of]…
⍳⍤...
1
Red, 45 bytes
repeat n 256[print[p: n - 1 p xor 128 - 128]]
Try it online!
Uses @Arnauld's formula
3
05AB1E, 11 10 bytes
₅ÝƵQݱ«ø»
-1 byte thanks to @ovs.
Uses single spaces and newlines as delimiters for the output.
Try it online.
Explanation:
₅Ý # Push a list in the range [0,255]
ƵQÝ # Push a list in the range [0,127]
 # Bifurcate it; short for Duplicate & Reverse copy
± # Get the bitwise-NOT of each (n → -n-1)
« ...
3
C (gcc), 54 50 47 bytes
i;main(){for(;i<256;printf("%u,%hhd ",i++,i));}
Try it online!
Explanation
i; // Static variable initialized to 0
main() {
for(;i<256; // While the variable is <= 255:
printf("%u,%hhd ",i++,i));
// Output the original, then the char-...
4
JavaScript (ES6), 41 40 bytes
Saved 1 byte thanks to @ovs
Output format: 0,0 1,1 ... 255,-1.
f=n=>n>>8?'':[~~n,128+~n^127]+' '+f(-~n)
Try it online!
Or maybe 39 bytes or even 38 bytes by abusing the loose output format. (But it's ugly.)
Commented
f = // f is a recursive function
n => // taking a counter n, ...
1
QuadR, 40 bytes
Uses ∪ and ¯ instead of U and -
⍎⍵
\[
\(
,
\)
]
(
(1↓
{⍺+0,⍳0⌈⍵-⍺}
-1)
)
Try it online or try them all!
Execute (⍎) it (⍵) after simple transliteration to APL:
In
Out
Explanation
[
(
Open group
(
(1↓
Open group but with first element (the start) removed
,
{⍺+0,⍳0⌈⍵-⍺}
Anonymous infix lambda to compute range (see below)
)
-1)
Close ...
1
PowerShell for Windows, 133 114 bytes
$(switch -r($args){'\('{$l++}','{$l+=+$n}'\)'{$n-=1}'\)|]'{$l..$n*($l-le$n)
$l=0}\D{$n=''}'\d|-'{$n+=$_}})|sort -u
Try it online!
Less golfed:
$numbers = switch -Regex ($args){
'\(' {$left++}
',' {$left+=+$num}
'\)' {$num-=1}
'\)|]' {$left..$num*($left -le $num);$left=0}
'\D' {$num=''}
...
0
05AB1E, 28 bytes
'U¡ε¦¨',¡ŸÙy„()©Ã®"¦¨"‡.V}˜ê
Try it online or verify all test cases.
Explanation:
'U¡ '# Split the (implicit) input-string on "U"
ε # Map over each interval:
¦¨ # Remove the first and last characters (the "[]()")
',¡ '# Split on ","
...
1
PowerShell, 137 133 bytes
-4 bytes thanks to Mazzy, who has an even cooler PS solution to this challenge!
$r="'|% rep* '"
"'('+('$args$r(','[1+$r)','-1]$r]','))$r[','(($r,' '),($r`U' ',')+',0)'"|iex|iex|%{$l,$n=$_;$l..$n*($n-ge$l)}|sort -u
Try it online!
2
Pip, 14 bytes
LNABq/LNt//1+1
https://tio.run/##K8gs@P/fx8/RqVDfx69EX99Q2/D/f0MDczNjC1NjCwA
Explanation
LN natural log of... (change of base becasue this is the only log function they had)
ABq the absolute value of the input...
/ divided by...
LNt the natural log of 10... (change of base)
//1 ...
0
AWK, 33 bytes
$1=int(log(($1?$1^2:1)^.5)/2.3)+1
Try it online!
Basically, this will print the integer part of the log base 10 of the input, plus one. 2.3 is a working approximation of natural log of 10. To deal with negative numbers, it takes the square root of the power of two. If input is zero, returns 1 instead, so the pattern is different from 0, which ...
1
R, 148 bytes
function(s)unique(unlist(lapply(parse(t=chartr("[],","():",gsub(",(-?\\d))",",(\\1-1))",gsub("\\((-?\\d)","((1+\\1)",el(strsplit(s,"U")))))),eval)))
Try it online!
If there's a shorter way, I can't seem to think of it.
1
Zsh, 89 84 bytes
for i (${(s"U")1}){seq $[${${i%,*}#?}+1-#i&1] $[${${i#*,}%?}-!(##$i[-1]&4)]}|sort -u
Try it online!
2
JavaScript (ES6), 104 bytes
Returns a set.
s=>s.replace(/(\[?)(-?\d+),(-?\d+)(]?)/g,(_,a,b,c,d)=>(g=_=>b>c-!d||g(S.add(b++)))(b-=-!a),S=new Set)&&S
Try it online!
Commented
s => // s = input sequence
s.replace( // match in s all occurrences of:
/(\[?)(-?\d+),(-?\d+)(]?)/g, // (\[?) ...
2
Jelly, 22 bytes
ṣ”UµØ(fOị⁾ṖḊvṖḊVr/Ʋ)ẎQ
A monadic Link accepting a list of characters which yields a list of integers.
Try it online! (Footer formats the resulting Jelly list as a Python list.)
Or see the test-suite.
How?
ṣ”UµØ(fOị⁾ṖḊvṖḊVr/Ʋ)ẎQ - Link: list of characters, S
”U - literal character = 'U'
ṣ - split (S) ...
0
APL (Dyalog Unicode), 107 62 bytes
⍎'\]' '\[' ',' '\(' '\)'⎕R')' '(' '{⍺+⍳0⌈1+⍵-⍺}' '(1↓' '-1)'⊢⎕
Try it online!
Does a huge regex replacement and then executes the string.
uses ∪ and ¯ for input.
Uses ⎕IO←0 (0-indexing)
-45 bytes by stealing a ton from Adám's QuadR answer(It has a well-written explanation, go upvote it!).
0
Haskell, 194 180 bytes
import Data.List
a=init
l=drop 1
t""=[]
t c=nub$i v++t(l n)where(v,n)=span(/='U')c
i('(':c)=l$i('[':c)
i c|last c==')'=a$i$a c++"]"|1<2=f$span(/=',')c
f(_:o,_:w)=[read$o..read$a w]
Try it online!
2
J, 60 bytes
[:~.@;'U'<@([:({:+[:i.0>.-/)(']('={:,{.)+3 1".@;@{;:);._1@,]
Try it online!
'U' ...;._1@, ] - Prepend 'U', to the input ] and cut it using the first character ;._1, ie U, as a delimiter, applying the verb defined by ... to each piece. Now we'll breakdown what's in ...:
First we turn it into boxed words ;:, so for example '(_5,2]' ...
2
Ruby -nl, 69 bytes
p eval"[*#{gsub /[(),U-\]]/,?(=>'1+',?)=>'-1',?,=>'..',?U=>',*'}]|[]"
Try it online!
5
C++ (gcc), 139 138 134 104 101 100 bytes
Uses C++20 features which aren't enabled by default in GCC, but are standard.
int f(auto&s,auto&o){int x,y;for(char c,d;s>>c>>x>>d>>y>>d;s>>d)for(y-=d<42;y>x-c%2;o.insert(y--));}
Try it online!
Uses streams because I wanted to simulate>>a>>merge>>...
1
Wolfram Language (Mathematica), 66 bytes
ToExpression[(#/.{"["->"Range[","("->"Range[1+",")"->"-1]"})<>""]&
Try it online!
Input a list of characters.
1
Charcoal, 46 bytes
FE⪪SU⪪ι,F…⁺IΦ§ι⁰λ№§ι⁰(⁺I⮌Φ⮌§ι¹λ№§ι¹]F¬№υκ⊞υκIυ
Try it online! Link is to verbose version of code. Uses U as the union operator. Explanation:
FE⪪SU⪪ι,
Split the input on U and then split each part on , and loop over each pair of parts.
F…⁺IΦ§ι⁰λ№§ι⁰(⁺I⮌Φ⮌§ι¹λ№§ι¹]
Form a range from the integer in the first part (excluding the first ...
5
Jelly, 23 bytes
ṣ”Uµṙ1“,()“rḊṖ”yḟØ[V)FQ
Try it online!
Blegh
-1 byte thanks to ovs
How it works
ṣ”Uµṙ1“,()“rḊṖ”yḟØ[V)FQ - Main link. Takes a string S on the left
ṣ”U - Split S on "U"
µ ) - Over each section R in the split S:
ṙ1 - Rotate by once, shifting the first character to the end
...
answered Jan 4 at 22:33
caird coinheringaahing
22.1k55 gold badges5151 silver badges157157 bronze badges
2
Perl 5 -MList::Util=uniq -FU, 53 bytes
say for uniq map{($a,$b)=/-?\d+/g;$a+/\(/..$b-/\)/}@F
Try it online!
7
Python 3, 79 bytes
lambda s:eval('{*range%s}'%s.translate({91:40,40:'(1+',93:'+1)',85:',*range'}))
Try it online!
-6 bytes thanks to ovs
-3 bytes thanks to dingledooper
1
Scala, 125 bytes
(n,x,i)=>1 to 1<<30 find(c=>Seq.fill(x)(1 to c map(Math.pow(_,i))takeWhile(_<=c)).flatten.combinations(x).count(_.sum==c)==n)
Try it online!
Approach: for each candidate integer c, generate a list of x copies of i-powered integers lower than c, and count whether n (distinct!) combinations of length x sum up to c.
Performance:...
1
JavaScript (Node.js), 127 bytes
a=>a.map(s=>s.replace(/[a-z]+/g,s=>t+=Buffer(`3!S~0?.']5~g)~~~#I*~{2"&+,/-~4~$%q~(1`)[parseInt(s,36)%620%546%115%37]-33,t=0)|t)
Try it online!
Below is a summary of the hash function, which gives a result in \$[0..36]\$ with a few unused slots.
string | base 36 -> dec. | mod 620 | mod 546 | mod 115 |...
3
Jelly, 12 bytes
œċ³*⁴§ċ⁼⁵µ1#
A full program which (given enough time!) prints the result if it exists. Inputs are \$x\$, \$i\$, \$n\$.
Try it online! (Too slow for most of the test cases.)
How?
Brute-force (and an inefficient one at that):
œċ³*⁴§ċ⁼⁵µ1# - Main Link: x
µ1# - Count up starting with k=x until 1 truthy result, then yield k, using:
³ ...
answered Dec 29 '20 at 2:11
Jonathan Allan
78.9k55 gold badges5656 silver badges224224 bronze badges
5
05AB1E, 15 bytes
∞.ΔL¹m²ã€{ÙOy¢Q
Try it online!
This is really slow, inserting ¹zmï after .Δ limits the search space significantly: Try it online!
Commented:
# Inputs ¹=i, ²=x, ³=n
∞.Δ # find the first natural number k that satisfies:
L # each of the range [1..k]
¹m # raised to the i-th power
...
4
R + gtools, 84 bytes
function(n,x,i){while(sum(rowSums(gtools::combinations(+T,x,re=1)^i)==T)<n)T=T+1;+T}
Try it online!
Counts T up from 1, calculating the number of combinations of x numbers ≤T whose ith powers sum to T.
Stops at the first number that gives (at least) n combinations.
function(n,x,i){
# variable T (TRUE) ...
3
Wolfram Language (Mathematica), 79 bytes
(n=1;While[Length@Select[PowersRepresentations[n++,#2,#3],#~FreeQ~0&]!=#];n-1)&
Try it online!
2
Wolfram Language, 110 108 bytes
Try it online!
(i=0;While[Length@FindInstance[i++==Tr[(v=Unique[]~Table~#2)^#3]&&LessEqual@@v,v,PositiveIntegers,#]!=#];i)&
Top 50 recent answers are included
Related Tags
integer × 340code-golf × 318
number × 101
math × 70
sequence × 68
arithmetic × 52
array-manipulation × 34
string × 25
number-theory × 24
decision-problem × 21
primes × 16
ascii-art × 13
matrix × 11
binary × 11
base-conversion × 11
code-challenge × 10
kolmogorov-complexity × 8
geometry × 8
sorting × 7
bitwise × 7
division × 7
fastest-code × 6
palindrome × 6
polynomials × 6
restricted-source × 5