PE-CN / pe-cn-comments

2 stars 0 forks source link

Problem 17 | Project Euler | #19

Open sx349 opened 4 years ago

sx349 commented 4 years ago

https://pe-cn.github.io/17/

Problem 17 Number letter countsIf the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1

TerenceG-cn commented 3 years ago
number_word = {
    0: "",
    1: "one",
    2: "two",
    3: "three",
    4: "four",
    5: "five",
    6: "six",
    7: "seven",
    8: "eight",
    9: "nine",
    10: "ten",
    11: "eleven",
    12: "twelve",
    13: "thirteen",
    14: "fourteen",
    15: "fifteen",
    16: "sixteen",
    17: "seventeen",
    18: "eighteen",
    19: "nineteen",
    20: "twenty",
    30: "thirty",
    40: "forty",
    50: "fifty",
    60: "sixty",
    70: "seventy",
    80: "eighty",
    90: "ninety", 
    100: "hundred",
    1000: "one thousand",
}

letter_count = 0

for i in range(1, 1001):
    word = ""
    flag = 0
    if i // 1000 == 1:
        word += number_word[1000] + " "
        i = i % 1000
    if i // 100 > 0:
        word += number_word[i // 100] + " " + number_word[100]
        i = i % 100
        if i > 0:
            flag = 1
    if i // 10 > 1:
        if flag == 1:
            word += " and "
        word += number_word[i % 100 // 10 * 10] + "-" + number_word[i % 10]
    else:
        if flag == 1:
            word += " and "
        word += number_word[i % 100]
    # print(word)
    format_word = word.replace(" ", "").replace("-", "")
    letter_count += len(format_word)
print(letter_count)
p-gp commented 2 years ago
#include <stdio.h>

int f1(int i);
int f2(int i);
int count(int i);

int main() {
    int sum=0;
    for(int i=1; i<=1000; ++i){
        sum+=count(i);
    }
    printf("%d \n",sum);

    return 0;
}

int count(int i) {
    int a=1;
    while(1){
        if(i/1000>0) {
            break;
        } else if(i/100>0) {
            a=2;
            break;
        } else {
            a=3;
            break;
        }
    }
    int sum=0, t, re, and=0;
    re=i;
    switch(a) {
        case 1: {
            t=re/1000;
            sum+=f1(t);
            sum+=8; //thousand
            and=1;
            re=re%1000;
        }
        case 2: {
            if(t=re/100>0){
                sum+=f1(t);
                sum+=7; //hundred
                and=1;
                re=re%100;           
            }
        }
        case 3: {
            if(re==0){
                break;
            }else if((t=re)<20) {
                sum+=f1(t);
                if(and==1)
                    sum+=3;  //and
            }else {
                t=re/10;
                sum+=f2(t);
                sum+=f1(re%10);
                if(and==1)
                    sum+=3;  //and                
            }
        }    
    }

    return sum;
}

int f1(int i) {
    switch(i) {
        case 0: return 0;
        case 1: return 3;
        case 2: return 3;
        case 3: return 5;
        case 4: return 4;
        case 5: return 4;
        case 6: return 3;
        case 7: return 5;
        case 8: return 5;
        case 9: return 4;
        case 10: return 3;
        case 11: return 6;
        case 12: return 6;
        case 13: return 8;
        case 14: return 8;
        case 15: return 7;
        case 16: return 7;
        case 17: return 9;
        case 18: return 8;
        case 19: return 8;
    }

    return 0;
}

int f2(int i) {
    switch(i) {
        case 2: return 6;   //20
        case 3: return 6;   //30
        case 4: return 5;   //40
        case 5: return 5;   //50
        case 6: return 5;   //60
        case 7: return 7;   //70
        case 8: return 6;   //80
        case 9: return 6;   //90
    }

    return 0;
}
ScandiumLin commented 2 years ago

Mathematica

ans = 0; For[i = 1, i <= 1000, i++, k = IntegerName[i, "Words"]; a = ToCharacterCode[k, "Unicode"]; a = DeleteCases[a, 8208]; a = DeleteCases[a, 32]; ans = ans + Length[a]]

ans=ans + 9 99 3

AdwardAllan commented 1 year ago

number_word = Dict(
    0=> "",
    1=> "one",
    2=> "two",
    3=> "three",
    4=> "four",
    5=> "five",
    6=> "six",
    7=> "seven",
    8=> "eight",
    9=> "nine",
    10=> "ten",
    11=> "eleven",
    12=> "twelve",
    13=> "thirteen",
    14=> "fourteen",
    15=> "fifteen",
    16=> "sixteen",
    17=> "seventeen",
    18=> "eighteen",
    19=> "nineteen",
    20=> "twenty",
    30=> "thirty",
    40=> "forty",
    50=> "fifty",
    60=> "sixty",
    70=> "seventy",
    80=> "eighty",
    90=> "ninety", 
    100=> "hundred",
    1000=> "one thousand",
);

letter_count = 0

for i in range(1, 1000)
    word = ""
    flag = 0
    if i ÷ 1000 == 1
        word *= number_word[1000] * " "
        i = i % 1000
    end
    if i ÷ 100 > 0
        word *= number_word[i ÷ 100] * " " * number_word[100]
        i = i % 100
        if i > 0
            flag = 1
        end
    end
    if i ÷ 10 > 1
        if flag == 1
            word *= " and "
        end
        if i % 10 == 0
            word *= number_word[i % 100 ÷ 10 * 10] * "" * number_word[i % 10] 
        else
            word *= number_word[i % 100 ÷ 10 * 10] * "-" * number_word[i % 10] 
        end  
    else
        if flag == 1
            word *= " and "
        end
        word *= number_word[i % 100]
    end
    #println(word)
    format_word = replace(word," "=>"","-"=>"")
    letter_count += length(format_word)
end

println(letter_count)