Project Euler #17 Counting the string!

Talk about any languages right here. Share and discuss source, but don't expect your homework to be done for you.
Post Reply
Dude!!!
Hacker in Training
Posts: 78
Joined: Sun Dec 28, 2008 9:59 pm

Project Euler #17 Counting the string!

Post by Dude!!! » Sat Sep 05, 2009 3:48 pm

Project Euler #17. I only did 1-40 since I don't have a lot of time to write all the words. C# version!

Code: Select all

using System;
    class Program
    {
        static void Main(string[] args)
        {
            int totalString=0;
            string[] count = new string[] { "one", "two", "three", "four", "five", "six", "seven","eight","nine", "ten",
                "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
                "twenty", "twentyone", "twentytwo", "twentythree", "twentyfour", "twentyfive", "twentysix",         "twentyseven",
                "twentyeight", "twentynine", "thirty", "thirtyone", "thirtytwo", "thirtythree", "thirtyfour", "thirtyfive",
                "thirtysix", "thirtyseven", "thirtyeight", "thirtynine","forty"};
            for (int x = 0; x < count.Length; x++)
            {
                totalString += count[x].Length;
            }
            Console.WriteLine("String length for 1-40 is: {0}", totalString);
            Console.ReadLine();
        }
    }

User avatar
IceDane
Because I Can
Posts: 2652
Joined: Wed May 12, 2004 9:25 am

Re: Project Euler #17 Counting the string!

Post by IceDane » Sun Sep 06, 2009 2:13 am

So this isn't a full solution? Why post it then?

Anyway, you don't want to be typing in each word and counting it like that - you want to create a class that uses some algorithm to do it for you - and it's not that hard at all. Just think about it. How are numbers constructed?

Any number over 20 is composed of certain words.

User avatar
foldingstock
htd0rg lieutenant
Posts: 300
Joined: Sat Aug 16, 2008 10:38 pm

Re: Project Euler #17 Counting the string!

Post by foldingstock » Sun Sep 06, 2009 6:28 am

Icedane is right. Something like this is all you would need:

Code: Select all

        digits = [None, 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
                  'eight', 'nine']
        teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
                 'sixteen', 'seventeen', 'eighteen', 'nineteen']
        tens = [None, None, 'twenty', 'thirty', 'forty', 'fifty', 'sixty',
                'seventy', 'eighty', 'ninety']

User avatar
foldingstock
htd0rg lieutenant
Posts: 300
Joined: Sat Aug 16, 2008 10:38 pm

Re: Project Euler #17 Counting the string!

Post by foldingstock » Tue Sep 22, 2009 2:04 pm

Actually, I'll take it one step further. Why write out all of the numbers if you don't have to? Check this out:

Code: Select all

#include <stdio.h>
#define letters 1000
int count(int l);

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

}
int count(int l){
	int d=6;
	int y; //first digit
	int x; //second digit
	int z; //third digit

	y=l/100;
	x=l%100/10;
	z=l%10;
	if(y>0){
		d+=10;
		if(x>0 || z>0) d+=3;
		if(y>2 && y!=6) d+=1;
		if((6<y && y<9) || y==3) d+=1;
	}
	if(x==1){
		d+=2;
		if(z==0) d-=5;
		if(z==7) d+=1;
		if(0<z && z<3) d-=2;
		if(4<z && z<7) d-=1;
	}else{
		if(3<x && x<7) d-=1;
		if(x==7) d+=1;
		if(x==0) d-=6;
		if(z>0) d+=3;
		if(z>2 && z!=6) d+=1;
		if((6<z && z<9) || z==3) d+=1;
	}
	return d;
}

Tarantula12
n00b
Posts: 1
Joined: Thu Jun 27, 2013 9:46 am

Re: Project Euler #17 Counting the string!

Post by Tarantula12 » Thu Jun 27, 2013 10:02 am

foldingstock wrote:Actually, I'll take it one step further. Why write out all of the numbers if you don't have to? Check this out:

Code: Select all

#include <stdio.h>
#define letters 1000
int count(int l);

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

}
int count(int l){
	int d=6;
	int y; //first digit
	int x; //second digit
	int z; //third digit

	y=l/100;
	x=l%100/10;
	z=l%10;
	if(y>0){
		d+=10;
		if(x>0 || z>0) d+=3;
		if(y>2 && y!=6) d+=1;
		if((6<y && y<9) || y==3) d+=1;
	}
	if(x==1){
		d+=2;
		if(z==0) d-=5;
		if(z==7) d+=1;
		if(0<z && z<3) d-=2;
		if(4<z && z<7) d-=1;
	}else{
		if(3<x && x<7) d-=1;
		if(x==7) d+=1;
		if(x==0) d-=6;
		if(z>0) d+=3;
		if(z>2 && z!=6) d+=1;
		if((6<z && z<9) || z==3) d+=1;
	}
	return d;
}
why d=6? what does 6 means? could you explain a little bit? for newbies?

Post Reply