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

C# (.NET Core), (削除) 192 (削除ここまで) 187 + 23 bytes

-5 bytes thanks to TheLethalCoder

b=>n=>new int[n].Select((_,a)=>{var g=Convert.ToString(a+1,2);return(long)g.Reverse().SelectMany((x,i)=>Enumerable.Repeat(x,b?i+1:g.Length-i)).Select((x,i)=>x>48?Math.Pow(2,i):0).Sum();})

Byte count also includes:

namespace System.Linq{}

Try it online!

Input: left is true, right is false

Explanation:

b => n => // Take two inputs (bool and int)
 new int[n].Select((_, a) => { // Create new collection the size of n
 var g = Convert.ToString(a + 1, 2); // Take every number in sequence 1..n and convert to base 2 (in a string)
 return (long)g.Reverse() // Reverse the bits
 .SelectMany((x, i) => // Replace every bit with a collection of its repeats, then flatten the result
 Enumerable.Repeat(x, b ? i + 1 : g.Length - i))
 .Select((x, i) => x > 48 ? Math.Pow(2, i) : 0)
 .Sum(); // Replace every bit with a corresponding power of 2, then sum
 })

C# (.NET Core), (削除) 192 (削除ここまで) 187 + 23 bytes

-5 bytes thanks to TheLethalCoder

b=>n=>new int[n].Select((_,a)=>{var g=Convert.ToString(a+1,2);return(long)g.Reverse().SelectMany((x,i)=>Enumerable.Repeat(x,b?i+1:g.Length-i)).Select((x,i)=>x>48?Math.Pow(2,i):0).Sum();})

Byte count also includes:

namespace System.Linq{}

Try it online!

Input: left is true, right is false

Explanation:

b => n => // Take two inputs (bool and int)
 new int[n].Select((_, a) => { // Create new collection the size of n
 var g = Convert.ToString(a + 1, 2); // Take every number in sequence 1..n and convert to base 2 (in a string)
 return (long)g.Reverse() // Reverse the bits
 .SelectMany((x, i) => // Replace every bit with a collection of its repeats, then flatten the result
 Enumerable.Repeat(x, b ? i + 1 : g.Length - i))
 .Select((x, i) => x > 48 ? Math.Pow(2, i) : 0)
 .Sum(); // Replace every bit with a corresponding power of 2, then sum
 })

C# (.NET Core), (削除) 192 (削除ここまで) 187 + 23 bytes

-5 bytes thanks to TheLethalCoder

b=>n=>new int[n].Select((_,a)=>{var g=Convert.ToString(a+1,2);return(long)g.Reverse().SelectMany((x,i)=>Enumerable.Repeat(x,b?i+1:g.Length-i)).Select((x,i)=>x>48?Math.Pow(2,i):0).Sum();})

Byte count also includes:

namespace System.Linq{}

Try it online!

Input: left is true, right is false

Explanation:

b => n => // Take two inputs (bool and int)
 new int[n].Select((_, a) => { // Create new collection the size of n
 var g = Convert.ToString(a + 1, 2); // Take every number in sequence 1..n and convert to base 2 (in a string)
 return (long)g.Reverse() // Reverse the bits
 .SelectMany((x, i) => // Replace every bit with a collection of its repeats, then flatten the result
 Enumerable.Repeat(x, b ? i + 1 : g.Length - i))
 .Select((x, i) => x > 48 ? Math.Pow(2, i) : 0)
 .Sum(); // Replace every bit with a corresponding power of 2, then sum
 })
added 907 characters in body
Source Link

C# (.NET Core), 192(削除) 192 (削除ここまで) 187 + 23 bytes

-5 bytes thanks to TheLethalCoder

b=>n=>Enumerableb=>n=>new int[n].RangeSelect(1(_,na).Select(a=>=>{var g=Convert.ToString(aa+1,2);return(long)g.Reverse().SelectMany((x,i)=>Enumerable.Repeat(x,b?i+1:g.Length-i)).Select((x,i)=>x>48?Math.Pow(2,i):0).Sum();})

Byte count also includes:

namespace System.Linq{}

Try it online! Try it online!

Input: left is true, right is false

Explanation:

b => n => // Take two inputs (bool and int)
 new int[n].Select((_, a) => { // Create new collection the size of n
 var g = Convert.ToString(a + 1, 2); // Take every number in sequence 1..n and convert to base 2 (in a string)
 return (long)g.Reverse() // Reverse the bits
 .SelectMany((x, i) => // Replace every bit with a collection of its repeats, then flatten the result
 Enumerable.Repeat(x, b ? i + 1 : g.Length - i))
 .Select((x, i) => x > 48 ? Math.Pow(2, i) : 0)
 .Sum(); // Replace every bit with a corresponding power of 2, then sum
 })

C# (.NET Core), 192 + 23 bytes

b=>n=>Enumerable.Range(1,n).Select(a=>{var g=Convert.ToString(a,2);return(long)g.Reverse().SelectMany((x,i)=>Enumerable.Repeat(x,b?i+1:g.Length-i)).Select((x,i)=>x>48?Math.Pow(2,i):0).Sum();})

Byte count also includes:

namespace System.Linq{}

Try it online!

Input: left is true, right is false

C# (.NET Core), (削除) 192 (削除ここまで) 187 + 23 bytes

-5 bytes thanks to TheLethalCoder

b=>n=>new int[n].Select((_,a)=>{var g=Convert.ToString(a+1,2);return(long)g.Reverse().SelectMany((x,i)=>Enumerable.Repeat(x,b?i+1:g.Length-i)).Select((x,i)=>x>48?Math.Pow(2,i):0).Sum();})

Byte count also includes:

namespace System.Linq{}

Try it online!

Input: left is true, right is false

Explanation:

b => n => // Take two inputs (bool and int)
 new int[n].Select((_, a) => { // Create new collection the size of n
 var g = Convert.ToString(a + 1, 2); // Take every number in sequence 1..n and convert to base 2 (in a string)
 return (long)g.Reverse() // Reverse the bits
 .SelectMany((x, i) => // Replace every bit with a collection of its repeats, then flatten the result
 Enumerable.Repeat(x, b ? i + 1 : g.Length - i))
 .Select((x, i) => x > 48 ? Math.Pow(2, i) : 0)
 .Sum(); // Replace every bit with a corresponding power of 2, then sum
 })
Source Link

C# (.NET Core), 192 + 23 bytes

b=>n=>Enumerable.Range(1,n).Select(a=>{var g=Convert.ToString(a,2);return(long)g.Reverse().SelectMany((x,i)=>Enumerable.Repeat(x,b?i+1:g.Length-i)).Select((x,i)=>x>48?Math.Pow(2,i):0).Sum();})

Byte count also includes:

namespace System.Linq{}

Try it online!

Input: left is true, right is false

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