Remove Ad, Sign Up
Register to Remove Ad
Register to Remove Ad
Remove Ad, Sign Up
Register to Remove Ad
Register to Remove Ad
Signup for Free!
-More Features-
-Far Less Ads-
About   Users   Help
Users & Guests Online
On Page: 1
Directory: 70
Entire Site: 3 & 807
Page Staff: pokemon x, pennylessz, Barathemos, tgags123, alexanyways, supercool22, RavusRat,
04-23-24 05:18 AM

Forum Links

Thread Information

Views
763
Replies
6
Rating
1
Status
CLOSED
Thread
Creator
jack3604
11-26-14 08:12 PM
Last
Post
jack3604
12-07-14 06:36 PM
Additional Thread Details
Views: 253
Today: 0
Users: 0 unique

Thread Actions

Thread Closed
New Thread
New Poll
Order
 

Euler Project Problem 17

 

11-26-14 08:12 PM
jack3604 is Offline
| ID: 1108408 | 8 Words

jack3604
Level: 35


POSTS: 167/254
POST EXP: 22119
LVL EXP: 277606
CP: 784.7
VIZ: 20977

Likes: 1  Dislikes: 0
Here is the problem: https://projecteuler.net/problem=17

Ok, so I have the number 1 to 99 done and working perfectly. The problem now is anything over 100...

Basically my code is not doing what I would expect it to (as always) and I have absolutely no idea why and I need some help figuring it out. When it gets into the hundreds it prints nothing for 100, 200, 300, etc and then for 101 it prints "one hundred-one", and it's supposed it print "one hundred and one". And then when it get to 111 it print "one hundred-one" still...  Sorry for such long code...

This is C# being used in Visual Studio 2013


http://pastebin.com/5n8Xiktg

Thanks in advanced...
Here is the problem: https://projecteuler.net/problem=17

Ok, so I have the number 1 to 99 done and working perfectly. The problem now is anything over 100...

Basically my code is not doing what I would expect it to (as always) and I have absolutely no idea why and I need some help figuring it out. When it gets into the hundreds it prints nothing for 100, 200, 300, etc and then for 101 it prints "one hundred-one", and it's supposed it print "one hundred and one". And then when it get to 111 it print "one hundred-one" still...  Sorry for such long code...

This is C# being used in Visual Studio 2013


http://pastebin.com/5n8Xiktg

Thanks in advanced...
Trusted Member
Zander's adopted son


Affected by 'Laziness Syndrome'

Registered: 06-08-11
Location: Florida
Last Post: 3112 days
Last Active: 713 days

(edited by jack3604 on 11-26-14 08:30 PM)     Post Rating: 1   Liked By: soxfan849,

11-27-14 12:37 AM
soxfan849 is Offline
| ID: 1108489 | 271 Words

soxfan849
Level: 77


POSTS: 1454/1490
POST EXP: 106261
LVL EXP: 4006373
CP: 5193.6
VIZ: 222680

Likes: 0  Dislikes: 0
That's definitely tough to follow, and I think the problem can be solved in a more methodical way - I'll work on a solution myself and post it whenever I finish it. No promises, though! Visual Studio is taking its time installing for me.

But as for your issue, I'll take a stab at it. I might be missing some things, because I'm doing this without a compiler. But there are a couple of problems with your code. The first one you seem to have noticed, and fixed for most cases. You're converting a number to a string to grab a digit, but then you're comparing that digit, which is a char, to a number. This issue occurred more than once, which is why you see the same error for 101 and for 111. Your second error is due to a later if statement.

I'll use your example of 111, and ignore the fact that you're comparing a char to an int again (since it wouldn't work properly even if that wasn't the case). When you say if(x.ToString()[1] != 0 || x.ToString()[1] != 1), you're saying that if the second digit is not 0 (1 is not 0, so this is true) OR (we don't really need to continue, we're already going to execute whatever's inside the if statement) the second digit is not 1 (it is, but that's irrelevant) then execute. This if statement is where the "-one" is appended to your string.

By the way, converting integers to chars the way you are isn't really necessary. You could just enclose them in single quotes. For instance, Convert.ToChar(1) could just be '1'.
That's definitely tough to follow, and I think the problem can be solved in a more methodical way - I'll work on a solution myself and post it whenever I finish it. No promises, though! Visual Studio is taking its time installing for me.

But as for your issue, I'll take a stab at it. I might be missing some things, because I'm doing this without a compiler. But there are a couple of problems with your code. The first one you seem to have noticed, and fixed for most cases. You're converting a number to a string to grab a digit, but then you're comparing that digit, which is a char, to a number. This issue occurred more than once, which is why you see the same error for 101 and for 111. Your second error is due to a later if statement.

I'll use your example of 111, and ignore the fact that you're comparing a char to an int again (since it wouldn't work properly even if that wasn't the case). When you say if(x.ToString()[1] != 0 || x.ToString()[1] != 1), you're saying that if the second digit is not 0 (1 is not 0, so this is true) OR (we don't really need to continue, we're already going to execute whatever's inside the if statement) the second digit is not 1 (it is, but that's irrelevant) then execute. This if statement is where the "-one" is appended to your string.

By the way, converting integers to chars the way you are isn't really necessary. You could just enclose them in single quotes. For instance, Convert.ToChar(1) could just be '1'.
Vizzed Elite
The Reaper


Affected by 'Laziness Syndrome'

Registered: 01-09-11
Location: soxfan849
Last Post: 2716 days
Last Active: 2553 days

11-27-14 01:37 AM
Juliet is Offline
| ID: 1108501 | 62 Words

Juliet
Level: 149


POSTS: 6018/6750
POST EXP: 348455
LVL EXP: 40986158
CP: 10708.7
VIZ: 1377896

Likes: 0  Dislikes: 0
One advice: Once you start typing the same things or even copy pasting, it's high time you change your code into something cleaner or more efficient. That long line of 'if' and 'else if' could easily be changed to switch statements. I'll try finding my Java version of this lol, or if I can't find it, I'll try redoing the problem.
One advice: Once you start typing the same things or even copy pasting, it's high time you change your code into something cleaner or more efficient. That long line of 'if' and 'else if' could easily be changed to switch statements. I'll try finding my Java version of this lol, or if I can't find it, I'll try redoing the problem.
Vizzed Elite

3rd Place in the July 2009 VCS Competition!




Affected by 'Laziness Syndrome'

Registered: 05-10-09
Location: Manila, PH (Asia)
Last Post: 1568 days
Last Active: 118 days

11-27-14 03:40 PM
jack3604 is Offline
| ID: 1108703 | 75 Words

jack3604
Level: 35


POSTS: 168/254
POST EXP: 22119
LVL EXP: 277606
CP: 784.7
VIZ: 20977

Likes: 0  Dislikes: 0
soxfan849 : I don't understand how the if(x.ToString()[1] != 0 || x.ToString()[1] != 1) could possibly add "-one" to the word though because I have the if statement even before that when it says "if(x.ToString()[1]==0)" and then the rest of the code is nested for ease of reading. Why isn't this triggering for 101 or even in the statement after for 111? It's like that part of the code is magically cut out before it compiles it.
soxfan849 : I don't understand how the if(x.ToString()[1] != 0 || x.ToString()[1] != 1) could possibly add "-one" to the word though because I have the if statement even before that when it says "if(x.ToString()[1]==0)" and then the rest of the code is nested for ease of reading. Why isn't this triggering for 101 or even in the statement after for 111? It's like that part of the code is magically cut out before it compiles it.
Trusted Member
Zander's adopted son


Affected by 'Laziness Syndrome'

Registered: 06-08-11
Location: Florida
Last Post: 3112 days
Last Active: 713 days

11-27-14 10:54 PM
soxfan849 is Offline
| ID: 1108790 | 49 Words

soxfan849
Level: 77


POSTS: 1456/1490
POST EXP: 106261
LVL EXP: 4006373
CP: 5193.6
VIZ: 222680

Likes: 0  Dislikes: 0
jack3604 : Your first if statement won't execute, because it's asking if a character is equal to an integer, which can never be the case. And your second if statement isn't an if-else chained with the first one, so it doesn't matter if the first one has executed or not.

jack3604 : Your first if statement won't execute, because it's asking if a character is equal to an integer, which can never be the case. And your second if statement isn't an if-else chained with the first one, so it doesn't matter if the first one has executed or not.

Vizzed Elite
The Reaper


Affected by 'Laziness Syndrome'

Registered: 01-09-11
Location: soxfan849
Last Post: 2716 days
Last Active: 2553 days

12-04-14 10:30 PM
soxfan849 is Offline
| ID: 1110989 | 631 Words

soxfan849
Level: 77


POSTS: 1457/1490
POST EXP: 106261
LVL EXP: 4006373
CP: 5193.6
VIZ: 222680

Likes: 0  Dislikes: 0
I finally got some time to work on this (that I didn't then spend doing something else). I did it twice, in two completely different ways. The first way was way more complicated, but way more efficient. The second way was way easier to follow, so I used it to debug my first solution and come up with the right answer. Then I wrote a function in both and ran each 999,999 times and compared the run time just to see how much more efficient my first solution really was. The second one completed in just under 2 minutes. The first one completed in just under 2 seconds. A huge difference. Anyway, here are my two solutions.

https://dl.dropboxusercontent.com/u/11407669/Euler/17/firstSolution.txt
https://dl.dropboxusercontent.com/u/11407669/Euler/17/secondSolution.txt

I'll go over the first one, because that's the more interesting and more difficult one to follow. Though I can explain how the second one works too, if you want. Anyway, start in the main function and follow along.

The first thing I did was create a dictionary containing each unique number from 1 to 1,000. Because there aren't many of them, this was easy to do. The dictionary uses the actual number as the key, and the length of the string version of the number as the value. I did this to simply count the number of characters rather than having to print them out. This decision was made largely due to the fact that the method used to solve the problem makes it impossible to print values out in small steps.

Next I initialized some variables. I set max to 1,000 since that's what the problem asks for. It would be possible to solve for a higher number, but I didn't so anything bigger than 1,000 will be treated as 1,000 by the program anyway. I set isTesting to true, because the last time I ran it I was checking how efficient it was. And I set testCount to 999,999. That is the number of times the solution is run when performing a speed test. Then I either run the speed test, or I run the actual solution. I'll just go over the solution since that's the more interesting part.

So here we go. First, I create a couple variables. The variable result contains the total number of characters, and count is used to iterate over prefixes for the word hundred (so if you count to 199, the word one will be counted for each instance of the words one hundred). Then I check to see if the max is at least 1,000. If it is, I calculate for 999 and then add the length of "one thousand" to the results.

After that, I calculate the number of hundreds there are in max. For example, if the number is 934 then this number will be 9. Then, if max is under 100, the solution is easy and can be calculated and returned. Otherwise the function continues. It then calculates the number of times 1-99 need to be counted. Then it calculates anything left over. For example again, 934 will count 1-99 9 times, and then counts 1-34 again. This part doesn't work right now, though. I solved it that way initially, but right now it can't calculate for anything less than 1-99.

After that I count the number of times the words "hundred" and "and" occur. Yet again I'll use 934. The word "hundred" occurs 100 times for each 100 until 900, while the word "and" occurs 99. Then it also has to be calculated for 900-934. Finally, the prefix for each hundred is calculated and the result is returned.

Hopefully that made sense. Feel free to ask anything if it didn't, or share any thoughts. I'd be interested in hearing how your solution has been coming along as well.

I finally got some time to work on this (that I didn't then spend doing something else). I did it twice, in two completely different ways. The first way was way more complicated, but way more efficient. The second way was way easier to follow, so I used it to debug my first solution and come up with the right answer. Then I wrote a function in both and ran each 999,999 times and compared the run time just to see how much more efficient my first solution really was. The second one completed in just under 2 minutes. The first one completed in just under 2 seconds. A huge difference. Anyway, here are my two solutions.

https://dl.dropboxusercontent.com/u/11407669/Euler/17/firstSolution.txt
https://dl.dropboxusercontent.com/u/11407669/Euler/17/secondSolution.txt

I'll go over the first one, because that's the more interesting and more difficult one to follow. Though I can explain how the second one works too, if you want. Anyway, start in the main function and follow along.

The first thing I did was create a dictionary containing each unique number from 1 to 1,000. Because there aren't many of them, this was easy to do. The dictionary uses the actual number as the key, and the length of the string version of the number as the value. I did this to simply count the number of characters rather than having to print them out. This decision was made largely due to the fact that the method used to solve the problem makes it impossible to print values out in small steps.

Next I initialized some variables. I set max to 1,000 since that's what the problem asks for. It would be possible to solve for a higher number, but I didn't so anything bigger than 1,000 will be treated as 1,000 by the program anyway. I set isTesting to true, because the last time I ran it I was checking how efficient it was. And I set testCount to 999,999. That is the number of times the solution is run when performing a speed test. Then I either run the speed test, or I run the actual solution. I'll just go over the solution since that's the more interesting part.

So here we go. First, I create a couple variables. The variable result contains the total number of characters, and count is used to iterate over prefixes for the word hundred (so if you count to 199, the word one will be counted for each instance of the words one hundred). Then I check to see if the max is at least 1,000. If it is, I calculate for 999 and then add the length of "one thousand" to the results.

After that, I calculate the number of hundreds there are in max. For example, if the number is 934 then this number will be 9. Then, if max is under 100, the solution is easy and can be calculated and returned. Otherwise the function continues. It then calculates the number of times 1-99 need to be counted. Then it calculates anything left over. For example again, 934 will count 1-99 9 times, and then counts 1-34 again. This part doesn't work right now, though. I solved it that way initially, but right now it can't calculate for anything less than 1-99.

After that I count the number of times the words "hundred" and "and" occur. Yet again I'll use 934. The word "hundred" occurs 100 times for each 100 until 900, while the word "and" occurs 99. Then it also has to be calculated for 900-934. Finally, the prefix for each hundred is calculated and the result is returned.

Hopefully that made sense. Feel free to ask anything if it didn't, or share any thoughts. I'd be interested in hearing how your solution has been coming along as well.

Vizzed Elite
The Reaper


Affected by 'Laziness Syndrome'

Registered: 01-09-11
Location: soxfan849
Last Post: 2716 days
Last Active: 2553 days

12-07-14 06:36 PM
jack3604 is Offline
| ID: 1112091 | 67 Words

jack3604
Level: 35


POSTS: 173/254
POST EXP: 22119
LVL EXP: 277606
CP: 784.7
VIZ: 20977

Likes: 0  Dislikes: 0
soxfan849 : Thanks for sharing your solution. I was just starting to realize what I was having to use a dictionary, and now I know that that's what I'm supposed to do. But, I actually am currently taking a break from the Euler Project so that when I start doing it again I'll have a fresh prospective instead of doing what I've been doing (which isn't working ).
soxfan849 : Thanks for sharing your solution. I was just starting to realize what I was having to use a dictionary, and now I know that that's what I'm supposed to do. But, I actually am currently taking a break from the Euler Project so that when I start doing it again I'll have a fresh prospective instead of doing what I've been doing (which isn't working ).
Trusted Member
Zander's adopted son


Affected by 'Laziness Syndrome'

Registered: 06-08-11
Location: Florida
Last Post: 3112 days
Last Active: 713 days

Links

Page Comments


This page has no comments

Adblocker detected!

Vizzed.com is very expensive to keep alive! The Ads pay for the servers.

Vizzed has 3 TB worth of games and 1 TB worth of music.  This site is free to use but the ads barely pay for the monthly server fees.  If too many more people use ad block, the site cannot survive.

We prioritize the community over the site profits.  This is why we avoid using annoying (but high paying) ads like most other sites which include popups, obnoxious sounds and animations, malware, and other forms of intrusiveness.  We'll do our part to never resort to these types of ads, please do your part by helping support this site by adding Vizzed.com to your ad blocking whitelist.

×