Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// Complex Number Challenge Part 6, #8
// Created by Beck Lorsch on 2/17/20
import UIKit
import Foundation
//NOTE: Read in combination with written work for this problem
//creating an array to hold all 24th roots of 1, storing as a sub array [a,b] as in a+bi
var roots = [[Double]]()
var i = 1.0
while i<25.0 { //running through nth root of 1 formula (cos(360/n*i) + sin(360/n*i) where i is 1...n for n = 24
var a = cos(2 * Double.pi/24.0*i)
var b = sin(2 * Double.pi/24.0*i)
a = Double(round(a*1000)/1000) //round a to thousandths
b = Double(round(b*1000)/1000) //round b to thousandths
roots.append([a,b]) //add [a,b] to array of roots
//checking all 24th roots using complex number to the sixth formula (A bunch of real components + i(6a^5b + 6ab^5 - 20a^3b^3)). When 6a^5b... = 0, the imaginary portion of the root to the sixth will equal zero, thus the root to the sixth equals a real number
var solutions = [Double]()
for complexNumber in roots { //looping through roots stored as [a,b]
let a = complexNumber[0]
let b = complexNumber[1]
let sixth = (6*pow(a, 5)*b) + (6*pow(b, 5)*a) - (20*pow(a, 3)*pow(b, 3)) //using formula
if sixth < 0.0001 && sixth > -0.0001 { //if plugging into formula gives us near 0, it is a solution
solutions.append(sixth) //add to list of solutions
print(solutions.count) //number of solutions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment