#include <stdio.h>
int StarAndTriangleNumbers(int max); //protype
int main(int argc, char const *argv[]) {
StarAndTriangleNumbers(10);
return 0;
}
int StarAndTriangleNumbers(int max){
int maxTriangles = max*10; //we need more triangle numbers than star numbers to check against 10 isnt specific would work with anything bigger than 5 probably
int starNumbers[max]; //create an array to hold starnumbers
int triangleNumbers[maxTriangles]; //creates an array to hold triangle numbers
//create star numbers array
for (int i = 1; i <= max; i++){
starNumbers[i] = ( ( (6 * i) * ( i - 1) ) + 1 );
// printf("star number %d is %d\n",i, starNumbers[i]);
}
//create triangle numbers array
for (int i = 1; i <= maxTriangles; i++){
int num = 0;
for (int j = 1; j <= i; j++){
num += j;
}
triangleNumbers[i] = num;
// printf("triangle number %d is %d\n", i, triangleNumbers[i]);
}
int matches[max]; //hold indcies of which ever star number matches a triangle numbers
int y = 0;
//simple binary search algorithm.
for (int i = 1; i <= max; i++){
for (int j = 1; j < maxTriangles; j++){
if(starNumbers[i] == triangleNumbers[j]){
matches[y] = i;
y++;
}
}
}
//print out the numbers
for (int i = 0; i < (y - 1); i++) {
printf("%d,", starNumbers[matches[i]]);
}
printf("%d\n", starNumbers[matches[y-1]]);
return 0;
}
Comments
0 B
|👍
/👎