Last active
November 19, 2023 01:10
-
-
Save evilthreads669966/b3e373963c1a29d81e5a3977ef1348b3 to your computer and use it in GitHub Desktop.
line slope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.* | |
import java.util.concurrent.ConcurrentHashMap | |
import java.util.concurrent.Executors | |
import java.util.concurrent.Future | |
import java.util.concurrent.ThreadPoolExecutor | |
import kotlin.math.abs | |
fun main(){ | |
val pointA = Point(0,4) | |
val pointB = Point(-2,5) | |
val result = slope(pointB,pointA) | |
println(result) | |
} | |
fun slope(first: Point, second: Point): Fraction{ | |
val rise = second.y - first.y | |
val run = second.x - first.x | |
val fraction = Fraction(rise, run) | |
fraction.reduce() | |
return fraction | |
} | |
class Fraction (var numerator: Int, var denominator: Int){ | |
fun reduce(){ | |
try{ | |
val gcf = gcf(sortedSetOf(abs(denominator.toLong()),abs(numerator.toLong()))).gcf.toInt() | |
denominator = denominator / gcf | |
numerator = numerator /gcf | |
}catch (e: NumberZeroException){ } | |
} | |
override fun toString(): String { | |
return "$numerator/$denominator" | |
} | |
} | |
class Point(val x: Int, val y: Int){ | |
override fun toString(): String { | |
return "$x,$y" | |
} | |
} | |
@Throws(NumberZeroException::class, NegativeNumberException::class) | |
fun gcf(numbers: SortedSet<Long>): FactorResult<Long> { | |
numbers.forEach { | |
if(it == 0L) throw NumberZeroException() | |
if(it < 0L) throw NegativeNumberException() | |
} | |
val factors = ConcurrentHashMap<Long, MutableList<Long>>() | |
numbers.forEach { num -> | |
factors[num] = mutableListOf() | |
} | |
var pool = Executors.newFixedThreadPool(numbers.size) as ThreadPoolExecutor | |
val tasks = mutableListOf<Future<*>>() | |
numbers.forEach { num -> | |
val task = pool.submit { | |
for (i in 1..num) { | |
if (num % i == 0L) { | |
synchronized(factors){ | |
factors[num]!!.add(i) | |
} | |
} | |
} | |
} | |
tasks.add(task) | |
} | |
tasks.forEach { it.get() } | |
if (numbers.size == 1) { | |
factors[numbers.first()]!! | |
return FactorResult(factors[numbers.first()]!!.last(), factors[numbers.first()]!!, factors) | |
} | |
val commonFactors = Collections.synchronizedList(mutableListOf<Long>()) | |
numbers.drop(1) | |
tasks.clear() | |
if(pool.maximumPoolSize < factors[factors.keys.first()]!!.size) | |
pool.maximumPoolSize = factors[factors.keys.first()]!!.size | |
factors[factors.keys.first()]!!.forEach { factor -> | |
val task = pool.submit { | |
var count = 0 | |
numbers.forEach { num -> | |
if (factors[num]!!.contains(factor)) | |
count++ | |
} | |
if (count == numbers.size) | |
synchronized(commonFactors){ | |
commonFactors.add(factor) | |
} | |
} | |
tasks.add(task) | |
} | |
tasks.forEach { it.get() } | |
val result = FactorResult<Long>(commonFactors.max(), commonFactors, factors) | |
return result | |
} | |
class FactorResult<T: Number>(val gcf: T, val commonFactors: List<T>, val factors: Map<T,List<T>>) | |
class NumberZeroException: Exception("Do not enter the number zero") | |
class NegativeNumberException: Exception("Do not enter negative numbers") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment