Input either 0 (for attracting magnetism) or 1 (for repelling magnetism) for the initial question. When set to 0: The ball will be "magnetically" attracted to the cursor. When set to 1: The ball will always "magnetically" repel the cursor.
The long square-root components are just the distance formula, to measure the distance between the cursor and the ball (the magnetic pull is based on the distance). Feel free to take this code if you want. This is probably among the easiest ways to achieve an effect like this, and it's fairly simple too.