Advertisement
candale

Euler

Nov 13th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. int is_prime(int n) {
  6. if(n <= 1) {
  7. return -1;
  8. }
  9.  
  10. int i;
  11. for(i = 2; i <= sqrt(n); i++) {
  12. if(n % i == 0) {
  13. return 0;
  14. }
  15. }
  16.  
  17. return 1;
  18. }
  19.  
  20. float euler_f(int n) {
  21. int last_prime = 0, i = 2;
  22. float e = n;
  23.  
  24. while(n != 1) {
  25. if(n % i == 0 && is_prime(i)) {
  26. if(last_prime != i) {
  27. e = e * (1.0 - 1.0 / i);
  28. last_prime = i;
  29. }
  30. n = n / i;
  31. } else {
  32. i += 1;
  33. }
  34. }
  35.  
  36. return e;
  37. }
  38.  
  39. int main(int argn, char ** argv) {
  40. int number = atoi(argv[1]);
  41. int bound = atoi(argv[2]);
  42. int i;
  43. printf("Numbers that match: ");
  44. for(i = 1; i < bound; i ++) {
  45. if(euler_f(i) == number) {
  46. printf("%d, ", i);
  47. }
  48. }
  49. printf("\n");
  50. return 0;
  51. }
  52.  
  53.  
  54. // The Euler function φ: N → N is a mapping associating to each positive integer n the number φ(n), which is the number of integers that are relatively prime to n.
  55. // For example φ(6) = 2 because only 1 and 5 are relatively prime to 6.
  56. // The formula for finding such a number is the following:
  57. // given n, φ(n) = n * (1 - 1 / P1) * ... * (1 - 1 / Pi), P1 to Pi being the set of prim factors of n
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement