Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C# by yoba ( 16 years ago )
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace blabla
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter N: ");
int n = int.Parse(Console.ReadLine());
Console.WriteLine("Numbers: ");
int i = 0;
foreach(var yoba in yobaGenerator(n))
{
Console.Write("{0}\t", yoba);
i++;
}
Console.WriteLine("\nTotal: {0}", i);
Console.ReadLine();
}
public static IEnumerable yobaGenerator(int n)
{
yield return "1";
int i = 2;
List<int> checkedNums = new List<int>();
while (i < n)
{
string iBin = DecToBin(i);
string nextBin = NextBeautiful(iBin);
int nextDec = BinToDec(nextBin);
checkedNums.Add(nextDec);
if (!checkedNums.Contains(i + 1)) yield return DecToBin(i + 1);
i++;
}
}
public static string NextBeautiful(string bin)
{
int dec = BinToDec(bin);
int toAdd = bin.Aggregate(0, (acc, x) => x == '1' ? acc + 1 : acc);
return DecToBin(dec + toAdd);
}
public static int BinToDec(string bin)
{
return Convert.ToInt32(bin, 2);
}
public static string DecToBin(int dec)
{
return Convert.ToString(dec, 2);
}
}
}
Revise this Paste