Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2, Sig, Sigo 3 years ago committed by GitHub
parent 0872c7b92f
commit 0d8758e1db
  1. BIN
      archives/21/current
  2. 50
      archives/21/src/main.c
  3. 72
      archives/21/src/utils.c
  4. 11
      archives/21/src/utils.h
  5. BIN
      current
  6. 109
      src/main.c

Binary file not shown.

@ -0,0 +1,50 @@
#include <stdio.h>
#include "utils.h"
/*
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.
https://projecteuler.net/problem=21
*/
int main(int argc,char**argv) {
int counter=1;
long totalSum=0;
while (counter<10000) {
//printf("Factor sum of %d: 1,",counter);
int factorSum=1;
int max=counter/2;
for (int i=2;i<max;i++) {
if (counter%i==0) {
factorSum+=i;
if (i!=(counter/i)) {
factorSum+=counter/i;
}
max=counter/i;
//printf("%d,%d,",i,counter/i);
}
}
//printf("\n");
int factorSum2=1;
max=factorSum/2;
for (int i=2;i<max;i++) {
if (factorSum%i==0) {
factorSum2+=i;
factorSum2+=factorSum/i;
max=factorSum/i;
}
}
if (factorSum!=factorSum2&&counter==factorSum2) {
printf("%d\n",counter);
totalSum+=counter;
}
counter++;
}
printf("\n\nAmicable Number sum: %ld",totalSum);
return 0;
}

@ -0,0 +1,72 @@
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
struct String add(struct String numb1, struct String numb2){
byte carryover=0;
int digitCount=0;
char*str = malloc(0);
if (numb1.length>=numb2.length) {
for (int offset=0;offset<numb1.length;offset++) {
str = realloc(str,++digitCount);
//printf("Digit count is now %d\n",digitCount);
if (numb2.length>offset) {
//printf("%c %c\n",numb1.str[numb1.length-offset-1],numb2.str[numb2.length-offset-1]);
int sum=((numb1.str[numb1.length-offset-1]-'0')+(numb2.str[numb2.length-offset-1]-'0'))+((carryover>0)?carryover--:0);
if (sum>=10) {
carryover=1;
sum-=10;
}
str[offset]=sum+'0';
} else {
str[offset]=numb1.str[numb1.length-offset-1];
}
}
} else {
for (int offset=0;offset<numb2.length;offset++) {
str = realloc(str,++digitCount);
//printf("Digit count is now %d\n",digitCount);
if (numb1.length>offset) {
//printf("%c %c\n",numb1.str[numb1.length-offset-1],numb2.str[numb2.length-offset-1]);
int sum=((numb1.str[numb1.length-offset-1]-'0')+(numb2.str[numb2.length-offset-1]-'0'))+((carryover>0)?carryover--:0);
if (sum>=10) {
carryover=1;
sum-=10;
}
str[offset]=sum+'0';
} else {
str[offset]=numb2.str[numb2.length-offset-1];
}
}
}
if (carryover>0) {
str = realloc(str,++digitCount);
str[digitCount-1]='1';
}
for (int i=0;i<digitCount/2;i++) {
char c = str[i];
char c2 = str[digitCount-i-1];
str[digitCount-i-1]=c;
str[i]=c2;
}
struct String newStr = {digitCount,str};
return newStr;
}
void printLongDoubleArr(int a,int b,long doubleArr[a][b]) {
for (int i=0;i<a;i++) {
for (int j=0;j<b;j++) {
printf("%ld\t",doubleArr[i][j]);
}
printf("\n");
}
}
void printIntDoubleArr(int a,int b,int doubleArr[a][b]) {
for (int i=0;i<a;i++) {
for (int j=0;j<b;j++) {
printf("%d\t",doubleArr[i][j]);
}
printf("\n");
}
}

@ -0,0 +1,11 @@
#define true 1
#define false 0
#define boolean char
#define byte char
struct String{
int length;
char*str;
};
struct String add(struct String numb1, struct String numb2);
void printLongDoubleArr(int a,int b,long doubleArr[a][b]);
void printIntDoubleArr(int a,int b,int doubleArr[a][b]);

Binary file not shown.

@ -2,88 +2,49 @@
#include "utils.h"
/*
You are given the following information, but you may prefer to do some research for yourself.
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair and each of a and b are called amicable numbers.
1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
https://projecteuler.net/problem=19
*/
enum Month {JANUARY=1,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER};
Evaluate the sum of all the amicable numbers under 10000.
int DaysInMonth(enum Month month,int year) {
switch (month) {
case JANUARY:
case MARCH:
case MAY:
case JULY:
case AUGUST:
case OCTOBER:
case DECEMBER:{
return 31;
}break;
case APRIL:
case JUNE:
case SEPTEMBER:
case NOVEMBER:{
return 30;
}break;
case FEBRUARY:{
return ((year%100==0)&&(year%400!=0))?28:(year%4==0)?29:28;
}break;
}
}
https://projecteuler.net/problem=21
*/
int main(int argc,char**argv) {
int day=1;
enum Month month=1;
int year=1900;
enum Weekday{
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
} weekday=MONDAY;
int sundayCount=0;
while((day!=31)||(month!=12)||(year!=2000)) {
printf("Date: %d/%d/%d\n",month,day,year);
if (year>=1901) {
//Start counting Sundays.
if (weekday==SUNDAY&&day==1) {
sundayCount++;
int counter=1;
long totalSum=0;
while (counter<10000) {
//printf("Factor sum of %d: 1,",counter);
int factorSum=1;
int max=counter/2;
for (int i=2;i<max;i++) {
if (counter%i==0) {
factorSum+=i;
if (i!=(counter/i)) {
factorSum+=counter/i;
}
max=counter/i;
//printf("%d,%d,",i,counter/i);
}
}
//printf("\n");
int factorSum2=1;
max=factorSum/2;
for (int i=2;i<max;i++) {
if (factorSum%i==0) {
factorSum2+=i;
factorSum2+=factorSum/i;
max=factorSum/i;
}
}
//Increment the day.
if (day+1<=DaysInMonth(month,year)) {
day++;
} else
if (month+1<=12){
//It's a new month.
month++;
day=1;
} else {
//It's a new year!
year++;
day=1;
month=1;
//printf("The year is now %d...\n",year);
if (factorSum!=factorSum2&&counter==factorSum2) {
printf("%d\n",counter);
totalSum+=counter;
}
weekday=((weekday+1)<=SATURDAY)?weekday+1:SUNDAY;
counter++;
}
printf("\n\n%d Sundays were on the 1st of the month.",sundayCount);
printf("\n\nAmicable Number sum: %ld",totalSum);
return 0;
}
Loading…
Cancel
Save