120 likes | 233 Views
Programming Training. Main Points to Review: - Problems with Primes. - Problems with Digits. - Problems with Arrays. Prime Numbers. Some Facts about primes: 1. A number p is prime is p has only 2 divisors 1 and p . 2. Even numbers >2 are not primes.
E N D
Programming Training Main Points to Review: - Problems with Primes. - Problems with Digits. - Problems with Arrays.
Prime Numbers Some Facts about primes: 1. A number p is prime is p has only 2 divisors 1 and p. 2. Even numbers >2 are not primes. 3. An odd number p>3 is not prime if it has a divisor d: 1<d<=sqrt(n). 4. 1 is not prime; 5. Small primes: 2,3,5,7,11,13,17,19,23, etc Do you get any pattern? How to test primality: 1. Eliminate directly the cases 1, 2, 3, multiple of 2 or 3. 2. Serch for an odd divisor of n amongst 3, 5, 7, …., sqrt(n) 3. If any the number is not prime. Recall: d is divisor of n iff n%d==0
Prime Numbers - Functions Prime Number Decomposition : intprime_decomp(long n, int p[], int pow[]){ int d, nr=0, count = 0, power; for(d=2;d<=n;d++) if(n%d==0 && isPrime(d)) { for(power=0;n%d==0;power++) { n=n/d; } p[count] = d; pow[count] = power; count++; } return; } Prime Number Testing: int isPrime(long p){ long d; ìnt ans; if(p==1) return 0; if(p==2 || p==3) return 1; if(p%2==0 || p%3==0) return 0; for(d=3;d<=sqrt(p);d=d+2) if(n%d==0) return 0; return 1; }
Example 1. Find all primes <=n. #include<stdio.h> #include<conio.h> #include<math.h> int isPrime(long p); int main (int args,char ** argc){ long n, i; // declare the variables printf("n=");scanf("%ld",&n); printf("2, "); for(i=3;i<=n;i=i+2) { if(isPrime(i)) { printf("%ld, ", i); } } return 1; }
The Eratosthenes Algorithm. The algorithm finds all primes less than a number n using sieving. We strart from a list that contains all the numbers 0,1,2,3, ….,n. Repeat removing from the list all the multiples of 2, 3, 5, etc. Example: Initial: List=(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, 19, 20) Multiples of 2: List=(0,1,2,3,0,5,0,7,0,9,0,11,0,13,0,15,0,17,0, 19, 0) Multiples of 3: List=(0,1,2,3,0,5,0,7,0,0,0,11,0,13,0,0,0,17,0, 19, 0) Multiples of 5 have been already removed. The list of primes is (2,3,5,7,,11,13,17,19). Facts: The most efficient solution for finding all primes.
#include<stdio.h> #include<conio.h> #include<math.h> int main (int args,char ** argc){ long n, i, d, primes[100000]; // declare the variables printf("n=");scanf("%ld",&n); for(i=0;i<=n;i++)primes[i]=i; for(d=2;d<=sqrt(n);d++) if(primes[d]>0) { for(i=2;i*d<=n;i++) primes[i*d]=0; } for(i=2;i<=n;i++) if(primes[i] >0) printf("%ld, ", i); return 1; }
An Old AIPO Competition Problem A integer number n is valuable if has lots of prime divisors. For example: 72=2^3*3^2 has only 2 prime divisors and it is less valuable than 30=2*3*5 that has 3 prime divisors. Given an array of integers find which number is the most valuable using the above rule. Example: a= (2,3,4,6,9) the result is 6 with 2 primes. For a number we need its prime number decomposition. int nr = prime_decomp(n, p, pow); nr number of prime divisors p array with nr elements representing the prime divisors pow array with nr elements representing the primes’ powers THIS IS A MAXIMUM PROBLEM.
#include<stdio.h> #include<conio.h> #include<math.h> int prime_decomp(long n, int p[], int pow[]); int main (int args,char ** argc){ long n, a[100000], max=-32000, pos=-1; int p[100], pow[100]; printf("n=");scanf("%ld",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { nr = prime_decomp(n, p, pow); if( max<nr) { max=nr;pos=i; } } printf("Most Valuable Nr: %d", a[pos]); return 1; }
Find the digits of an integer. If n is a long number then last=n % 10 is the last digit. n = n/10 is the new number from which we remove last. n = n*10 + digit is the new number with digit appended. If we repeat removing the last digit then we can find: - all the digits of the number. - number of digits. Example: n=45672941 last: 1 | 4 | 9 | 2 | … n: 4567294 | 456729 | 45672 | 4567 | … We repeat while the number is not fully done.
Find the reverse: int reverse(long n) { intreverse_n = 0; while(n!=0) { last = n % 10; n = n / 10; reverse_n = reverse_n * 10 + digit; } returnreverse_n;; } Find all digits: int digits(long n, int digit[]) { int last, count=0; while(n!=0) { last = n % 10; n = n / 10; digit[count] = last; cont ++; } returncount; }
To do List • Solve the HW problems. • Read some new information about primes from internet. • Find fancy primes.