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

added 34 characters in body
Source Link
Mayube
  • 11.8k
  • 3
  • 50
  • 93

C# (.NET Core), 5856 bytes

n=>{int a=0,i=1;fori=0;for(;i<n;i++;++i<n;)a+=i%2==0a+=i%2<1?-1:i/2+1;return a;}

Try it online! -2 bytes thanks to Kevin Crujssen

Try it online!

1 indexed. Returns a(n)

Ungolf'd:

int f(int n)
{
 // a needs to be outside the for loop's scope,
 // and it's golfier to also define i here
 int a = 0, i = 1;
 // basic for loop, no initializer because we already defined i
 for (; i++i < n; i++)
 {
 if (i%2 ==< 01) {
 // if i is even, subtract 1
 a -= 1;
 }
 else
 {
 // if i is odd, add (i / 2) + 1
 // this lets us handle X without defining another int
 a += i / 2 + 1;
 }
 }
 // a is the number at index n
 return a;
}

C# (.NET Core), 58 bytes

n=>{int a=0,i=1;for(;i<n;i++)a+=i%2==0?-1:i/2+1;return a;}

Try it online!

1 indexed. Returns a(n)

Ungolf'd:

int f(int n)
{
 // a needs to be outside the for loop's scope,
 // and it's golfier to also define i here
 int a = 0, i = 1;
 // basic for loop, no initializer because we already defined i
 for (; i < n; i++)
 {
 if (i%2 == 0) {
 // if i is even, subtract 1
 a -= 1;
 }
 else
 {
 // if i is odd, add (i / 2) + 1
 // this lets us handle X without defining another int
 a += i / 2 + 1;
 }
 }
 // a is the number at index n
 return a;
}

C# (.NET Core), 56 bytes

n=>{int a=0,i=0;for(;++i<n;)a+=i%2<1?-1:i/2+1;return a;}

-2 bytes thanks to Kevin Crujssen

Try it online!

1 indexed. Returns a(n)

Ungolf'd:

int f(int n)
{
 // a needs to be outside the for loop's scope,
 // and it's golfier to also define i here
 int a = 0, i = 1;
 // basic for loop, no initializer because we already defined i
 for (; ++i < n;)
 {
 if (i%2 < 1) {
 // if i is even, subtract 1
 a -= 1;
 }
 else
 {
 // if i is odd, add (i / 2) + 1
 // this lets us handle X without defining another int
 a += i / 2 + 1;
 }
 }
 // a is the number at index n
 return a;
}
deleted 5 characters in body
Source Link
Mayube
  • 11.8k
  • 3
  • 50
  • 93

C# (.NET Core), 6058 bytes

n=>{int a=0,i=1;for(;i<n;i++)a+=(i%2==0a+=i%2==0?-1:i/2+1);return2+1;return a;}

Try it online! Try it online!

1 indexed. Returns a(n)

Ungolf'd:

int f(int n)
{
 // a needs to be outside the for loop's scope,
 // and it's golfier to also define i here
 int a = 0, i = 1;
 // basic for loop, no initializer because we already defined i
 for (; i < n; i++)
 {
 if (i%2 == 0) {
 // if i is even, subtract 1
 a -= 1;
 }
 else
 {
 // if i is odd, add (i / 2) + 1
 // this lets us handle X without defining another int
 a += i / 2 + 1;
 }
 }
 // a is the number at index n
 return a;
}

C# (.NET Core), 60 bytes

n=>{int a=0,i=1;for(;i<n;i++)a+=(i%2==0?-1:i/2+1);return a;}

Try it online!

1 indexed. Returns a(n)

Ungolf'd:

int f(int n)
{
 // a needs to be outside the for loop's scope,
 // and it's golfier to also define i here
 int a = 0, i = 1;
 // basic for loop, no initializer because we already defined i
 for (; i < n; i++)
 {
 if (i%2 == 0) {
 // if i is even, subtract 1
 a -= 1;
 }
 else
 {
 // if i is odd, add (i / 2) + 1
 // this lets us handle X without defining another int
 a += i / 2 + 1;
 }
 }
 // a is the number at index n
 return a;
}

C# (.NET Core), 58 bytes

n=>{int a=0,i=1;for(;i<n;i++)a+=i%2==0?-1:i/2+1;return a;}

Try it online!

1 indexed. Returns a(n)

Ungolf'd:

int f(int n)
{
 // a needs to be outside the for loop's scope,
 // and it's golfier to also define i here
 int a = 0, i = 1;
 // basic for loop, no initializer because we already defined i
 for (; i < n; i++)
 {
 if (i%2 == 0) {
 // if i is even, subtract 1
 a -= 1;
 }
 else
 {
 // if i is odd, add (i / 2) + 1
 // this lets us handle X without defining another int
 a += i / 2 + 1;
 }
 }
 // a is the number at index n
 return a;
}
Source Link
Mayube
  • 11.8k
  • 3
  • 50
  • 93

C# (.NET Core), 60 bytes

n=>{int a=0,i=1;for(;i<n;i++)a+=(i%2==0?-1:i/2+1);return a;}

Try it online!

1 indexed. Returns a(n)

Ungolf'd:

int f(int n)
{
 // a needs to be outside the for loop's scope,
 // and it's golfier to also define i here
 int a = 0, i = 1;
 // basic for loop, no initializer because we already defined i
 for (; i < n; i++)
 {
 if (i%2 == 0) {
 // if i is even, subtract 1
 a -= 1;
 }
 else
 {
 // if i is odd, add (i / 2) + 1
 // this lets us handle X without defining another int
 a += i / 2 + 1;
 }
 }
 // a is the number at index n
 return a;
}

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