language: C++ 4.7.2 (gcc-4.7.2)
date: 955 days 4 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
 
int main(void)
{
  typedef unsigned long long ulong;
  ulong num = 17022805693386603001ULL;
 
  if (num<2)
    return 0;
 
  ulong workingNum=num;
 
  // Factor out factors of 2
  while (workingNum%2==0)
  {
    std::cout << "2 ";
    workingNum/=2;
  }
 
  // Factor out factors of 3
  while (workingNum%3==0)
  {
    std::cout << "3 ";
    workingNum/=3;
  }
 
  if (workingNum==1)
    return 0;
 
  // Factor out factors >=5
  ulong nextOffset=2;
  char nextShift = 1;
  ulong n = 5;
  ulong nn = 25;
  do {
    // Is workingNum divisible by n?
    if (workingNum%n==0)
    {
      // n is a factor!
      // so is the number multiplied by n to get workingNum
 
      // Insert n into the list of factors
      std::cout << n << ' ';
 
      // Divide working number by n
      workingNum/=n;
 
      // Test for done...
      if (workingNum==1)
        return 0;
 
      // Try n again
    }  
    else {
      nn += n << (nextShift+1) + 1<<(nextShift*2); // (n+b)^2 = n^2 + 2*n*b + b*2
      n += nextOffset;
      nextOffset ^= 6;
      nextShift ^= 3;
      // invariant: nn == n*n
      if (n & 0x100000000LL) break; // careful of integer wraparound in n^2
    }
  } while (nn <= workingNum);
 
  // workingNum is prime, add it to the list of factors        
  std::cout << workingNum << std::endl;
  return 0;
}
prog.cpp: In function ‘int main()’:
prog.cpp:55: warning: suggest parentheses around + or - inside shift