-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1578 from itsIapetus/WOC-Contributions
Kotlin Algorithms
- Loading branch information
Showing
3 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package kotlin.math | ||
|
||
// Area of a Square | ||
|
||
fun square(a: Double): Double { | ||
return a * a | ||
} | ||
|
||
// Area of a Reactangle | ||
|
||
fun rectangle(l: Double, b: Double): Double{ | ||
return l * b | ||
} | ||
|
||
// Area of a Circle | ||
|
||
fun circle(r: Double): Double{ | ||
return PI * r * r | ||
} | ||
|
||
// Area of a Triangle | ||
|
||
fun triangle(b: Double, h: Double): Double{ | ||
return b * h/2.0 | ||
} | ||
|
||
// Area of a Parallelogram | ||
|
||
fun parallelogram(b: Double, h: Double): Double{ | ||
return b * h | ||
} | ||
|
||
// Area of a Trapezium | ||
|
||
fun trapezium(a: Double, b:Double, h:Double): Double{ | ||
return (a+b)*h/2.0 | ||
} | ||
|
||
|
||
fun main() { | ||
println(square(2.0)) | ||
println(rectangle(2.0, 4.6)) | ||
println(circle(5.0)) | ||
println(triangle(2.0,3.0)) | ||
println(parallelogram(4.5,2.3)) | ||
println(trapezium(1.5,4.5,6.0)) | ||
} | ||
|
||
|
||
// Time Complexity - O(n) | ||
// Space Complexity - O(n) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package kotlin.math | ||
|
||
// Area of a Cube | ||
|
||
fun cube(a: Double): Double { | ||
return 6 * a * a | ||
} | ||
|
||
// Area of a Cuboid | ||
|
||
fun cuboid(l: Double, b: Double, h: Double): Double{ | ||
return 2 * ((l*b) + (b*h) + (h*l)) | ||
} | ||
|
||
// Area of a Cone | ||
|
||
fun cone(r: Double, l: Double): Double{ | ||
return (PI * r * r) + (PI * r * l) | ||
} | ||
|
||
// Area of a Cylinder | ||
|
||
fun cylinder(r: Double, h: Double): Double{ | ||
return (2.0 * (PI * r * r)) + (r * h) | ||
} | ||
|
||
// Area of a Sphere | ||
|
||
fun sphere(r: Double): Double{ | ||
return 4.0 * PI * r * r | ||
} | ||
// Area of a Hemi Sphere | ||
|
||
fun hemisphere(r: Double): Double{ | ||
return (2.0 * PI * r * r) + (PI * r * r) | ||
} | ||
|
||
|
||
fun main() { | ||
println(cube(2.0)) | ||
println(cuboid(2.0, 4.6, 3.5)) | ||
println(cone(5.0, 4.0)) | ||
println(cylinder(2.0,4.0)) | ||
println(sphere(4.5)) | ||
println(hemisphere(2.5)) | ||
} | ||
|
||
|
||
// Time Complexity - O(n) | ||
// Space Complexity - O(n) |
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