Decomposição em fatores primos
Publicado por Enzo de Brito Ferber 28/03/2009
[ Hits: 17.332 ]
Homepage: http://www.maximasonorizacao.com.br
Sim, eu tenho outro script desse aqui no VOL, mas esse está bem melhor (mais compacto e rapido).
Outra coisa, o código está em inglês como sempre, pois este site não é o unico que publico (e o ingles é a linguagem internacional da informática).
Então, happy coding!
// primes.c /* Enzo Ferber : <enzo@veloxmail.com.br> * * Decompose into Prime Factors a given number * * march 27 2009 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define START 2 // used by the main() function to print the factors int control; /* decom ( num ) * * @ num: number to decompose * * @ Return: return an int* containing all the prime factors */ int *decom ( int num ) { // ... register int i; int *primes = (int *) malloc ( sizeof (int) ); // Houston, we have a problem! if ( !primes ) exit (0); // set control variable control = 1; // START represents the first prime number, 2 for ( i = START; i <= num || num != 1 ; i++ ) { // ensures just an exact division while ( (num % i) == 0) { // I WANT MORE MEMORY, BITCH!!! primes = (int *) realloc ( primes, control * sizeof (int)); // Houston, we have a problem! if ( !primes) exit (0); // put the current prime factor into the list primes[control - 1] = i; control++; // set new number to be divided next num = num / i; } } // return the prime list return primes; } int main ( int argc, char **argv ) { // check for the correct argument if ( argc != 2 ) { // HowTo use a very complex program... printf ( "Usage: %s <number>\n", argv[0] ); return 0; } // begin the program if the arguments are correct register int i; // call the function to decompose into prime factors int *primes = decom ( atoi(argv[1]) ); // print prime list for ( i = 0; i < control - 1; i++ ) printf ( "%3d: %d\n", i + 1, primes[i] ); // free the memory free ( primes ); return 0; }
Algorítmo para Calcular Raiz Quadrada
Simples gerador de números primos
Testar o melhor método de organização C (inserção, bolha e shell-sort)
Kernel turbinado, compatibilidade em alta e debate sobre sustentabilidade: o dia no mundo Linux
Kernel turbinado e GNOME 49 dominam o giro do dia no mundo Linux
Novidades Linux: Kernel 6.17, distros em ebulição e segurança em foco
Kernel ganha novos linters Rust e distros avançam com recursos de IA
Adicionando o repositório backports no Debian 13 Trixie
Como definir um IP estático no Linux Debian
Jogos de Azar Online no Android em Portugal: Tudo o Que Precisa Saber (0)
Orientação para instalar o Warsaw - módulo de proteção do Banco do Bra... (6)
systemd-resol... precisa ser reiniciado periodicamente (5)
Alguém que utilize o Warsaw do BB no Ubuntu 24.04 [RESOLVIDO] (6)
Fedora não reconhece James Donkey 102 Wireless Gaming Mouse (2)