Skip to content

Moore's Voting Algorithm - Boyer-Moore Majority Voting Algorithm #1750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: updated code
  • Loading branch information
Mrinal Chauhan committed Oct 25, 2024
commit 1951f32f2bc36fa6588551dedcefb6e8df5d09ff
17 changes: 10 additions & 7 deletions Maths/MobiusFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
* @param {Integer} number
* @returns {Integer}
*/

import { PrimeFactors } from './PrimeFactors.js'

export const mobiusFunction = (number) => {
const primeFactorsArray = PrimeFactors(number)
if (number <= 0) {
throw new Error('Number must be greater than zero.')
}
return primeFactorsArray.length !== new Set(primeFactorsArray).size
? 0
: primeFactorsArray.length % 2 === 0
? 1
: -1

const primeFactorsArray = PrimeFactors(number)
const uniquePrimeFactors = new Set(primeFactorsArray)

// If there are duplicate factors, it means number is not square-free.
if (primeFactorsArray.length !== uniquePrimeFactors.size) {
return 0
}
return uniquePrimeFactors.size % 2 === 0 ? 1 : -1
}