Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2, Sig, Sigo 2 years ago committed by GitHub
parent c3cfd140bb
commit bbab1294fc
  1. BIN
      archives/5/current
  2. 29
      archives/5/src/main.c
  3. 7
      archives/5/src/utils.h
  4. BIN
      current
  5. 69
      src/main.c

Binary file not shown.

@ -0,0 +1,29 @@
#include <stdio.h>
#include "utils.h"
/*
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
https://projecteuler.net/problem=5
*/
int main(int argc,char**argv) {
int numb=1;
while (true) {
boolean isDivisible=true;
for (int i=1;i<=20;i++) {
if (numb%i!=0) {
isDivisible=false;
break;
}
}
if (isDivisible) {
printf("%d is divisible!",numb);
break;
} else {
numb++;
}
}
return 0;
}

@ -0,0 +1,7 @@
#define true 1
#define false 0
#define boolean char
struct String{
int length;
char*str;
};

Binary file not shown.

@ -2,65 +2,28 @@
#include "utils.h"
/*
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
Find the largest palindrome made from the product of two 3-digit numbers.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
https://projecteuler.net/problem=4
https://projecteuler.net/problem=5
*/
struct String numberToString(int numb) {
int placeValues=0;
int tempNumb=numb;
while (tempNumb>0) {
tempNumb/=10;
placeValues++;
}
char*finalStr=malloc(placeValues+1);
tempNumb=numb;
int marker=placeValues-1;
while (tempNumb>0) {
finalStr[marker--]='0'+(tempNumb%10);
tempNumb/=10;
}
struct String str={placeValues,finalStr};
return str;
}
boolean isPalindrome(struct String numb) {
int offset=0;
while (offset<numb.length/2) {
//printf("Compare %c to %c\n",numb.str[offset],numb.str[numb.length-offset-1]);
if (numb.str[offset]!=numb.str[numb.length-offset-1]) {
return false;
}
offset++;
}
return true;
}
int main(int argc,char**argv) {
int numb1=999;
int numb2=999;
int maxPal=0;
//printf("%s : %d",numberToString(numb1*numb2).str,numberToString(numb1*numb2).length);
for (numb1=999;numb1>0;numb1--) {
for (numb2=999;numb2>0;numb2--) {
struct String str = numberToString(numb1*numb2);
if (isPalindrome(str)) {
printf("%d is a Palindrome!\n",numb1*numb2);
if (maxPal<numb1*numb2) {
maxPal=numb1*numb2;
printf(" New MAX set to %d\n",maxPal);
}
int numb=1;
while (true) {
boolean isDivisible=true;
for (int i=1;i<=20;i++) {
if (numb%i!=0) {
isDivisible=false;
break;
}
free(str.str);
}
if (isDivisible) {
printf("%d is divisible!",numb);
break;
} else {
numb++;
}
}
printf("\n\nThe maximum palindrome is %d\n",maxPal);
return 0;
}
Loading…
Cancel
Save