summaryrefslogtreecommitdiff
path: root/common.c
blob: 69f87c5b69cc58a2b7d6fee4b0967abc1ec300a9 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdlib.h>
#include <string.h>

// returns list of primes up to limit; count stored in first element
extern unsigned long* primes(unsigned long limit)
{
	unsigned long i = 2; // start sieving with 2
	unsigned long pos, x;
	char* numbers = (char*) malloc(limit*sizeof(char));
	unsigned long* primes;
	unsigned long primecount = 0;
	memset(numbers, 1, limit);
	numbers[0] = 0;

	while(i*i <= limit)
	{
		// sieve current prime
		x = i*i;
		while(x <= limit)
		{
			numbers[x-1] = 0;
			x += i;
		}
		
		// search next prime
		pos = i;
		while(++pos <= limit)
		{
			if(numbers[pos-1] == 1)
				break;
		}
		i = pos;
	}

	// count primes
	pos = 0;
	while(++pos <= limit)
	{
		if(numbers[pos-1] == 1)
			primecount++;
	}

	// create list of primes
	primes = (unsigned long*) malloc((primecount+1)*sizeof(unsigned long)); // store count at first element
	primes[0] = primecount;
	pos = 0;
	x = 1;
	while(++pos <= limit)
	{
		if(numbers[pos-1] == 1)
			primes[x++] = pos;
	}

	free(numbers);
	return primes;
}

extern int isprime(unsigned long number, const unsigned long* primes)
{
	unsigned long count = primes[0];
	unsigned long pos = count/2 + 1;
	unsigned long dist = pos/2 + 1;
	unsigned long minpos = 0, maxpos = count;

	if(number > primes[count] || number < 2)
		return 0;

	// some kind of binary search...
	while(1)
	{
		if(number > primes[pos])
		{
			minpos = pos;
			pos += dist;
		}
		else if(number < primes[pos])
		{
			maxpos = pos;
			pos -= dist;
		}
		else
			return 1;

		dist /= 2;

		if(dist == 1)
		{
			while(++minpos <= maxpos)
				if(primes[minpos] == number)
					return 1;
			break;
		}
	}

	return 0;
}

extern unsigned long phi(unsigned long n, unsigned long* primelist)
{
	double product = (double) n;
	unsigned long i;

	//if(isprime(n, primelist))
	//	return n-1;

	for(i=1; i<=primelist[0]; i++)
	{
		unsigned long p = primelist[i];
		if(p > n)
			break;
		if(n % p != 0)
			continue;
		product *= (1 - 1.0/p);
	}
	return (unsigned long) product;
}

extern int ispermutation(unsigned long a, unsigned long b)
{
	char digits1[10];
	char digits2[10];
	memset(digits1, 0, 10);
	memset(digits2, 0, 10);

	while(a > 0 && b > 0)
	{
		char d1 = a % 10;
		char d2 = b % 10;
		digits1[d1]++;
		digits2[d2]++;
		a /= 10;
		b /= 10;
	}

	if(a > 0 || b > 0)
		return 0;

	if(memcmp(digits1, digits2, 10) == 0)
		return 1;

	return 0;
}

/*int main()
{
	long* p = primes(10000);
	//printf("%i\n", isprime(9973, p));
	int i;
	for(i=0; i<p[0]; i++)
		printf("%d: %d\n", p[i+1], isprime(p[i+1], p));
	free(p);
	return 0;
}*/