Sha256: d6bfcd74a5cce889e6dca54a8e8a0f4e79bd00960e95afe7aca586eadd64d60d

Contents?: true

Size: 564 Bytes

Versions: 71

Compression:

Stored size: 564 Bytes

Contents

#include "prime_factors.h"
#include <math.h>

/**
 * find prime factors: brute force version
 * https://en.wikipedia.org/wiki/Trial_division
 */
size_t find_factors(uint64_t n, uint64_t factors[static MAXFACTORS])
{
   int count = 0;
   while ((n % 2 == 0) && (count < MAXFACTORS)) {
      factors[count++] = 2;
      n /= 2;
   }

   for (uint64_t i = 3; (i <= sqrt(n)) && (count < MAXFACTORS); i += 2) {
      while (n % i == 0) {
         factors[count++] = i;
         n /= i;
      }
   }

   if (n > 2) {
      factors[count++] = n;
   }

   return count;
}

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.179 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.178 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.177 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.176 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.175 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.174 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.173 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.172 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.171 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.170 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.169 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.167 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.166 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.165 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.164 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.163 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.162 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.161 tracks/c/exercises/prime-factors/src/example.c
trackler-2.2.1.160 tracks/c/exercises/prime-factors/src/example.c