User:Kithira/Course Pages/CSCI 12/Assignment 2/Group 1/Homework 2: Difference between revisions

Content deleted Content added
Vargasr39 (talk | contribs)
No edit summary
m Replaced deprecated <source> tags with <syntaxhighlight>
 
Line 3:
Here is a program written in [[Python (programming language)]] that allows the user to input any positive integer and receive a list of the prime factors:
 
<sourcesyntaxhighlight lang="python">
from Factors import Factor
from PrimeNumber import Prime
Line 22:
if __name__ == '__main__':
_test()
</syntaxhighlight>
</source>
 
''Using Factor( ), the program goes through each factor of the designated number and checks if it is prime, using Prime( ). If True, the factor is added to the M list, then once all digits have been checked it returns the final list. Before allowing the user to input a number, it tests an input of 6 automatically to ensure any changes have not affected its functionality.''
Line 31:
Returns a list of all the factors of a number.
 
<sourcesyntaxhighlight lang="python">
def Factor(X):
result = []
Line 42:
n += 1
return result
</syntaxhighlight>
</source>
 
==== Prime ====
Returns a [[boolean]] (True/False) of whether a number is prime.
 
<sourcesyntaxhighlight lang="python">
from Factors import Factor
 
def Prime(X):
return bool( len( Factor(X) ) == 2 )
</syntaxhighlight>
</source>
 
This code relies upon the structure of the '''Factors''' program above, using the fact that the factors of a prime number are always 1 and itself.