1 墨瑟 德·布鲁因数(Moser de-Bruijn)
墨瑟 德·布鲁因数(Moser de-Bruijn)序列是将数字4(例如,1、4、16、64等)的不同幂相加得到的序列。
计算公式:
1) S(2 * n) = 4 * S(n) 2) S(2 * n + 1) = 4 * S(n) + 1 with S(0) = 0 and S(1) = 1
2 计算结果
3 文本格式源程序
using System;
namespace Legalsoft.Truffer.Algorithm
{
public static partial class Number_Sequence
{
public static int Moser_deBruijn_Number(int n)
{
if (n == 0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else if ((n % 2) == 0)
{
return 4 * Moser_deBruijn_Number(n / 2);
}
else if ((n % 2) == 1)
{
return 4 * Moser_deBruijn_Number(n / 2) + 1;
}
return 0;
}
public static int Moser_deBruijn_Number_Second(int n)
{
int[] dump = new int[n + 1];
dump[0] = 0;
if (n != 0)
{
dump[1] = 1;
}
for (int i = 2; i <= n; i++)
{
if ((i % 2) == 0)
{
dump[i] = 4 * dump[i / 2];
}
else
{
dump[i] = 4 * dump[i / 2] + 1;
}
}
return dump[n];
}
}
}
————————————————————————
POWER BY TRUFFER.CN
4 代码格式源代码
using System;namespace Legalsoft.Truffer.Algorithm
{public static partial class Number_Sequence{public static int Moser_deBruijn_Number(int n){if (n == 0){return 0;}else if (n == 1){return 1;}else if ((n % 2) == 0){return 4 * Moser_deBruijn_Number(n / 2);}else if ((n % 2) == 1){return 4 * Moser_deBruijn_Number(n / 2) + 1;}return 0;}public static int Moser_deBruijn_Number_Second(int n){int[] dump = new int[n + 1];dump[0] = 0;if (n != 0){dump[1] = 1;}for (int i = 2; i <= n; i++){if ((i % 2) == 0){dump[i] = 4 * dump[i / 2];}else{dump[i] = 4 * dump[i / 2] + 1;}}return dump[n];}}
}