parent
95aab4b49a
commit
46aac37f06
2 changed files with 103 additions and 0 deletions
@ -0,0 +1,68 @@ |
|||||||
|
#include<stdio.h> |
||||||
|
#include<stdlib.h> |
||||||
|
|
||||||
|
void findFactors(int num) |
||||||
|
{ |
||||||
|
int i, exponent = 0; |
||||||
|
printf("%d =", num); |
||||||
|
|
||||||
|
while (num % 2 == 0) |
||||||
|
{ |
||||||
|
exponent++; |
||||||
|
num /= 2; |
||||||
|
} |
||||||
|
if (exponent > 1) |
||||||
|
{ |
||||||
|
printf(" 2^%d *", exponent); |
||||||
|
}
|
||||||
|
else if (exponent == 1) |
||||||
|
{ |
||||||
|
printf(" 2 *"); |
||||||
|
} |
||||||
|
for (i = 3; i * i <= num; i += 2) |
||||||
|
{ |
||||||
|
exponent = 0; |
||||||
|
while (num % i == 0) |
||||||
|
{ |
||||||
|
exponent++; |
||||||
|
num /= i; |
||||||
|
} |
||||||
|
if (exponent > 1) |
||||||
|
{ |
||||||
|
printf(" %d^%d *", i, exponent); |
||||||
|
} |
||||||
|
else if (exponent == 1) |
||||||
|
{ |
||||||
|
printf(" %d *", i); |
||||||
|
} |
||||||
|
} |
||||||
|
if (num != 1) |
||||||
|
{ |
||||||
|
printf(" %d *", num); |
||||||
|
} |
||||||
|
printf("\b \n"); |
||||||
|
} |
||||||
|
|
||||||
|
int main(int argc, char *argv[]) |
||||||
|
{ |
||||||
|
int num; |
||||||
|
|
||||||
|
if (argc != 2) |
||||||
|
{ |
||||||
|
printf("Usage: %s <Integer>\n", argv[0]); |
||||||
|
return 1; |
||||||
|
} |
||||||
|
|
||||||
|
num = atoi(argv[1]); |
||||||
|
if (num <= 1) |
||||||
|
{ |
||||||
|
printf("%d\n", num); |
||||||
|
}
|
||||||
|
else
|
||||||
|
{ |
||||||
|
findFactors(num); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
import sys |
||||||
|
|
||||||
|
def findFactors(num): |
||||||
|
exponent = 0 |
||||||
|
print(f"{num} =", end="") |
||||||
|
while num % 2 == 0: |
||||||
|
exponent += 1 |
||||||
|
num //= 2 |
||||||
|
if exponent > 1: |
||||||
|
print(f" 2^{exponent} *", end="") |
||||||
|
elif exponent == 1: |
||||||
|
print(" 2 *", end="") |
||||||
|
for i in range(3, int(num**0.5) + 1, 2): |
||||||
|
exponent = 0 |
||||||
|
while num % i == 0: |
||||||
|
exponent += 1 |
||||||
|
num //= i |
||||||
|
if exponent > 1: |
||||||
|
print(f" {i}^{exponent} *", end="") |
||||||
|
elif exponent == 1: |
||||||
|
print(f" {i} *", end="") |
||||||
|
if num != 1: |
||||||
|
print(f" {num} *", end="") |
||||||
|
print("\b ") |
||||||
|
|
||||||
|
if __name__ == "__main__": |
||||||
|
if len(sys.argv) != 2: |
||||||
|
print("Usage: python prime.py <Integer>") |
||||||
|
sys.exit(1) |
||||||
|
|
||||||
|
num = int(sys.argv[1]) |
||||||
|
if num <= 1: |
||||||
|
print(f"{num}") |
||||||
|
else: |
||||||
|
findFactors(num) |
||||||
Loading…
Reference in new issue