/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to libssts/primes.c

  • Committer: Gustav Hartvigsson
  • Date: 2017-01-24 20:55:19 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20170124205519-gtr18o3dwbunrrnx
* Fixed the tests in the CMake file
* Made doxygen output static declarations.
* Started work on SApplication
* Played with BaseN.c
  * Now it is a lil' bit better
* Spilt defs.h
  * Added types.h
    * Started work on the full typesystem.
      (Still needs testing)
  * Added primes.[c,h]
    * Contains some static array with primes.
      ("Good" primes, and all primes up to 5 000.
    * And helper functions related to Primes (Needs Tests).
* fixed s_dynamic_array_dump_array.
  (The old version did not make much sense)
* removed some functions from DymanicArray.c
* fixed compiler warnings in Mainloop.c
* removed s_map_(de)serialize_json functions.
* Made s_thread_status_get_name be less prone to error
  (This due to the C11 standard not specifing what these
   values should be)
* fixed s_thread_run
* fixed s_threa_stop

  TODO:
* Write tests for the s_prime_* functions
* Write tests for the s_type_* functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "primes.h"
 
2
#include "defs.h"
 
3
#include "utils.h"
 
4
 
 
5
 
 
6
sboolean
 
7
s_prime_is (suint n) {
 
8
  
 
9
  
 
10
  if (n <= 1 || (n % 2 == 0  && n > 2) ) {
 
11
    return FALSE;
 
12
  }
 
13
  
 
14
  /*
 
15
   * Binary search in list.
 
16
   */
 
17
  if (n <= 4999) {
 
18
    return s_binary_search (SPrimeListLong, 0, S_PRIME_LIST_LONG_LEN, n);
 
19
  }
 
20
  
 
21
  /*
 
22
   * Since all non-primes are a product of two primes, we only need to check
 
23
   * a subset of all values.
 
24
   */
 
25
  for (sint i = 0; i < S_PRIME_LIST_LONG_LEN; i++){
 
26
    if (n % SPrimeListLong[i] == 0) {
 
27
      return FALSE;
 
28
    }
 
29
  }
 
30
  /*
 
31
   * if we exit the loop now, and n is less than the highest value in the list
 
32
   * squared, we have found a prime.
 
33
   */
 
34
  if (n <= 4999 * 4999 ) {
 
35
    return TRUE;
 
36
  }
 
37
  
 
38
  /*
 
39
   * Last chance to see if it is not a prime. This will be expensive!
 
40
   */
 
41
  for (sint i = 4999 + 1; i <= floor(sqrt(n)); i++) {
 
42
    if (n % i == 0) {
 
43
      return FALSE;
 
44
    }
 
45
  }
 
46
  
 
47
  return TRUE;
 
48
}