1 | /* |
---|
2 | * Copyright (C) 2004 Benjamin Otte <in7y118@public.uni-hamburg.de> |
---|
3 | * |
---|
4 | * This library is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU General Public |
---|
6 | * License as published by the Free Software Foundation; either |
---|
7 | * version 2 of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This library is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 | * General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public |
---|
15 | * License along with this library; if not, write to the Free |
---|
16 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
17 | */ |
---|
18 | |
---|
19 | #include <gst/gst.h> |
---|
20 | #include <stdlib.h> |
---|
21 | |
---|
22 | #define MAX_SIEVE 20 |
---|
23 | |
---|
24 | static void |
---|
25 | eratosthenes (GValue * sieve, gboolean up, int size) |
---|
26 | { |
---|
27 | guint i, j; |
---|
28 | GValue temp = { 0, }; |
---|
29 | GValue list = { 0, }; |
---|
30 | |
---|
31 | g_value_init (sieve, GST_TYPE_INT_RANGE); |
---|
32 | gst_value_set_int_range (sieve, 2, size * size); |
---|
33 | for (i = up ? 2 : size; up ? (i <= size) : (i >= 2); i += up ? 1 : -1) { |
---|
34 | g_value_init (&list, GST_TYPE_LIST); |
---|
35 | for (j = 2 * i; j <= size * size; j += i) { |
---|
36 | GValue v = { 0, }; |
---|
37 | |
---|
38 | g_value_init (&v, G_TYPE_INT); |
---|
39 | g_value_set_int (&v, j); |
---|
40 | gst_value_list_append_value (&list, &v); |
---|
41 | g_value_unset (&v); |
---|
42 | } |
---|
43 | gst_value_subtract (&temp, sieve, &list); |
---|
44 | g_value_unset (sieve); |
---|
45 | gst_value_init_and_copy (sieve, &temp); |
---|
46 | g_value_unset (&temp); |
---|
47 | g_value_unset (&list); |
---|
48 | /* g_print ("%2u: %s\n", i, gst_value_serialize (sieve)); */ |
---|
49 | } |
---|
50 | |
---|
51 | g_print ("%s\n", gst_value_serialize (sieve)); |
---|
52 | } |
---|
53 | |
---|
54 | gint |
---|
55 | main (gint argc, gchar ** argv) |
---|
56 | { |
---|
57 | GValue up = { 0, }; |
---|
58 | GValue down = { 0, }; |
---|
59 | guint size = MAX_SIEVE; |
---|
60 | |
---|
61 | gst_init (&argc, &argv); |
---|
62 | |
---|
63 | if (argc > 1) |
---|
64 | size = atol (argv[1]); |
---|
65 | |
---|
66 | eratosthenes (&up, TRUE, size); |
---|
67 | eratosthenes (&down, FALSE, size); |
---|
68 | |
---|
69 | g_assert (gst_value_compare (&up, &down) == GST_VALUE_EQUAL); |
---|
70 | return 0; |
---|
71 | } |
---|