forked from TheAlgorithms/Julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prime Check and Prime Factors (TheAlgorithms#42)
* prime check * prime factors
- Loading branch information
1 parent
75c8b3e
commit 50e15b4
Showing
4 changed files
with
109 additions
and
0 deletions.
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
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,40 @@ | ||
""" | ||
prime_check(number) | ||
Checks to see if a number is a prime or not | ||
A number is prime if it has exactly two factors: 1 and itself. | ||
# Example | ||
```julia | ||
prime_check(2) # returns true | ||
prime_check(3) # returns true | ||
prime_check(5) # returns true | ||
prime_check(7) # returns true | ||
prime_check(11) # returns true | ||
prime_check(13) # returns true | ||
prime_check(17) # returns true | ||
prime_check(19) # returns true | ||
prime_check(23) # returns true | ||
prime_check(29) # returns true | ||
``` | ||
Contributed By:- [Ashwani Rathee](https://github.com/ashwani-rathee) | ||
""" | ||
function prime_check(number) | ||
if 1 < number < 4 | ||
# 2, 3 are primes | ||
return true | ||
elseif number < 2 || number % 2 == 0 | ||
# Negative ,0,1, and even numbers except 2 are not primes | ||
return false | ||
end | ||
odd_numbers = collect(3:2:trunc(sqrt(number) + 1)) | ||
for i in odd_numbers | ||
if number % i == 0 | ||
return false | ||
end | ||
end | ||
return true | ||
end |
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,36 @@ | ||
""" | ||
prime_factors(number) | ||
Returns prime factors of `number` as a vector | ||
# Example | ||
```julia | ||
prime_factors(50) # returns [2,5,5] | ||
prime_factors(0) # returns [] | ||
prime_factors(100) # returns [2, 2, 5, 5] | ||
prime_factors(2560) # returns [2, 2, 2, 2, 2, 2, 2, 2, 2, 5] | ||
prime_factors(10^-2) # returns [] | ||
prime_factors(0.02) # returns [] | ||
prime_factors(10^-354) # returns [] | ||
prime_factors("hello") # returns error | ||
``` | ||
Contributed By:- [Ashwani Rathee](https://github.com/ashwani-rathee) | ||
""" | ||
function prime_factors(number) | ||
i = 2 | ||
factors = [] | ||
while i^2 <= number | ||
if number % i == 1 | ||
i += 1 | ||
else | ||
number ÷= i | ||
push!(factors, i) | ||
end | ||
end | ||
if number > 1 == 1 | ||
push!(factors, number) | ||
end | ||
return factors | ||
end |
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