Alger logo
𝔸𝕝𝕘𝕖𝕣
About

Sequence

// The Moser-de Bruijn sequence is the sequence obtained by
// adding up the distinct powers of the number 4 (For example 1, 4, 16, 64, etc).
// You can get more details on https://en.wikipedia.org/wiki/Moser%E2%80%93de_Bruijn_sequence.

package moserdebruijnsequence

func MoserDeBruijnSequence(number int) []int {
	sequence := []int{}

	for i := 0; i < number; i++ {
		res := generateNthTerm(i)
		sequence = append(sequence, res)
	}

	return sequence
}

func generateNthTerm(num int) int {
	if num == 0 || num == 1 {
		return num
	}

	//number is even
	if num%2 == 0 {
		return 4 * generateNthTerm(num/2)
	}

	//number is odd
	return 4*generateNthTerm(num/2) + 1
}
Algerlogo

© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.