tesseract v5.3.3.20231005
sample1.h File Reference

Go to the source code of this file.

Functions

int Factorial (int n)
 
bool IsPrime (int n)
 

Function Documentation

◆ Factorial()

int Factorial ( int  n)

Definition at line 35 of file sample1.cc.

35 {
36 int result = 1;
37 for (int i = 1; i <= n; i++) {
38 result *= i;
39 }
40
41 return result;
42}

◆ IsPrime()

bool IsPrime ( int  n)

Definition at line 45 of file sample1.cc.

45 {
46 // Trivial case 1: small numbers
47 if (n <= 1) return false;
48
49 // Trivial case 2: even numbers
50 if (n % 2 == 0) return n == 2;
51
52 // Now, we have that n is odd and n >= 3.
53
54 // Try to divide n by every odd number i, starting from 3
55 for (int i = 3; ; i += 2) {
56 // We only have to try i up to the square root of n
57 if (i > n/i) break;
58
59 // Now, we have i <= n/i < n.
60 // If n is divisible by i, n is not prime.
61 if (n % i == 0) return false;
62 }
63
64 // n has no integer factor in the range (1, n), and thus is prime.
65 return true;
66}