Skip to content

Commit

Permalink
Merge pull request #1133 from raccoote/master
Browse files Browse the repository at this point in the history
new factorization plugin
  • Loading branch information
pnhofmann committed Oct 12, 2023
2 parents 09db351 + 4099b9e commit 1cdb082
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions jarviscli/plugins/factor.py
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.")

0 comments on commit 1cdb082

Please sign in to comment.