Created
March 23, 2021 14:54
-
-
Save defrindr/7cc28004e5714a3a7d6869a9a6649919 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main() { | |
int N, i, * C; | |
printf("Masukkan nilai n : "); | |
scanf("%d", & N); | |
C = (int * ) malloc(N * sizeof(int)); | |
for (i = 0; i <= N; i++){ | |
if(i == 0){ | |
* C = 1; | |
}else{ | |
int temp = * (C + i - 1); | |
*(C + i) = 2 * temp + 1; | |
} | |
} | |
printf("C0 : %d\n", *C); | |
if(N == 0){ | |
return 0; | |
} | |
for (i = 1; i <= N; i++) | |
printf("C%d = 2 * C(%d - 1) + 1 = 2 * %d + 1 = %d \n", i, i, *(C + i - 1), *(C + i)); | |
free(C); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main(){ | |
int n, i, *S; | |
printf("Masukkan nilai n : "); | |
scanf("%d", & n); | |
S = (int*)malloc(n * sizeof(int)); | |
for(i = 0; i <= n; i++){ | |
if(i == 0){ | |
*S = 1; | |
}else{ | |
*(S + i) = *(S + i - 1) + i - 1; | |
} | |
} | |
printf("S0 = %d\n", *S); | |
if(n == 0){ | |
return 0; | |
} | |
for(i = 1; i <= n; i++){ | |
printf("S%d = S(%d-1) + %d - 1 = %d + %d = %d\n", | |
i, i, i, *(S + i - 1), (i - 1), *(S + i)); | |
} | |
} | |
//Sn = Sn-1 + n – 1 | |
//S0 = 1 | |
//S1 = S1-1 + 1 – 1 = S0 + 0 = 1 | |
//S2 = S2-1 + 2 – 1 = s1 + 1 = 2 | |
//S3 = S3-1 + 3 – 1 = s2 + 2 = 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main() { | |
int N, M, * prime, total_prime = 0, temp, number = 2, i; | |
printf("Masukkan nilai n : "); | |
scanf("%d", & N); | |
printf("Masukkan nilai m : "); | |
scanf("%d", & M); | |
prime = (int * ) malloc(N * sizeof(int)); | |
printf("\n%d Bilangan Prima Pertama yaitu : ", M); | |
*(prime + total_prime) = number; // default value | |
total_prime++; | |
while (number <= M ) { | |
temp = 1; | |
if(total_prime == N){ | |
// reallocation new memory | |
prime = (int * ) realloc(prime, sizeof(int) * M); | |
} | |
for(i = 0; i < total_prime; i++){ | |
if( number % *(prime+i) == 0 ) { | |
temp = 0; | |
} | |
} | |
if(temp){ | |
*(prime+total_prime) = number; | |
total_prime++; | |
} | |
number ++; | |
} | |
for(i=0;i<total_prime;i++) | |
printf("%d ", *(prime + i)); | |
free(prime); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment