-
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 #1133 from raccoote/master
new factorization plugin
- Loading branch information
Showing
1 changed file
with
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import math | ||
from plugin import plugin | ||
# A simple plugin to print all prime factors of a given number n | ||
|
||
@plugin("factor") | ||
def factor(jarvis, s): | ||
try: | ||
n = int(input("Enter a number for me to factorize: ")) | ||
|
||
factors = [] | ||
while n % 2 == 0: | ||
factors.append(2) | ||
n = n // 2 | ||
|
||
for i in range(3, int(math.sqrt(n)) + 1, 2): | ||
while n % i == 0: | ||
factors.append(i) | ||
n = n // i | ||
|
||
if n > 2: | ||
factors.append(n) # if n is prime | ||
|
||
result = ' x '.join(map(str, factors)) | ||
jarvis.say(result) | ||
except ValueError: | ||
jarvis.say("Invalid input. Please enter a positive integer.") |