Prime factors code in C and Python


Today someone spoke about prime factors and I thought it was something interesting to code. I needed to practice my C so I’ve coded it in C. Here it goes:

#include 
#define VMAX 25

int main()
{
    /*Prime numbers < 100 */
    int primes[VMAX] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
    int factors[VMAX] = {}; /*Prime factors*/
    int repeat[VMAX] = {}; /*Store times each factor repeats*/
    int a, i, n=0;

    printf("Type a number to find it's prime factors: \n");
    scanf("%d", &a);

    /*Start to divide the number by prime numbers while a is not 1*/
    for (i=0; i

I really like python but unfortunately I haven't had the time to code anything with python. Now it's learning C , then C++ and so on. But this time I wanted to code this in python also. I'm a bit out of practice and my brain was thinking in C. But here it goes.

#! /usr/bin/env/python
VMAX = 25

#Prime numbers < 100
primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
factors = [0]*VMAX #Prime factors
repeat = [0]*VMAX #Store times each factor repeats
i = 0
n = 0
k = 0

a = raw_input("Type a number to find it's prime factors:")

#Start to divide the number by prime numbers while a is not 1
for i in range(VMAX): 

    while ((a != 1) & (int (a) % primes[i] == 0)):
        
        a = int (a) / primes[i]
        
        print primes[i]
        factors[i] = primes[i] # The prime factor is the one being used
        
        n = n + 1 #*Count how many times each prime is used
        print n
    if (int (a) % primes[i] == 0):
        n = 0 #Reinitialize counter
    repeat[k] = n
    n = 0
    k = k + 1

print "Prime factors:"

#Print all factors in the array != 0
for i in range(VMAX):
    
    if (factors[i] !=0):
    #Since x ^ 0 = 1, we have to print x ^ 1 instead
        if (repeat[i] == 0):
            print factors[i]
        else: print "%s ^ %s " % (factors[i], repeat[i])
            

Um comentário a “Prime factors code in C and Python”

  1. VMAX = 25

    #Prime numbers < 100
    primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
    factors = [0]*VMAX #Prime factors
    repeat = [0]*VMAX #Store times each factor repeats
    i = 0
    n = 0
    k = 0
    spacer = 0
    a = raw_input("Type a number to find it's prime factors:")

    #Start to divide the number by prime numbers while a is not 1
    for i in range(VMAX):

    while ((a != 1) & (int (a) % primes[i] == 0)):

    a = int (a) / primes[i]

    factors[i] = primes[i] # The prime factor is the one being used

    n = n + 1 #*Count how many times each prime is used

    # formatting
    if(spacer == 0):
    print
    spacer = spacer + 1
    else:
    if(n == 1):
    print
    print
    print primes[i],
    print n,
    print " ",

    if (int (a) % primes[i] == 0):
    n = 0 #Reinitialize counter
    repeat[k] = n

    n = 0
    k = k + 1
    print
    print
    print "Prime factors:"

    #Print all factors in the array != 0
    for i in range(VMAX):

    if (factors[i] !=0):
    #Since x ^ 0 = 1, we have to print x ^ 1 instead
    if (repeat[i] == 0):
    print factors[i],
    else: print "%s ^ %s " % (factors[i], repeat[i]),

Deixe um comentário

Este site utiliza o Akismet para reduzir spam. Fica a saber como são processados os dados dos comentários.