Fedora Linux Support Community & Resources Center
  #1  
Old 19th October 2011, 07:56 AM
FuzzyDoom Offline
Registered User
 
Join Date: Nov 2010
Location: Oklahoma
Age: 19
Posts: 62
linuxchrome
C# List Help

So I'm currently trying to do projecteuler.com's problem 4 and am running into a wall with my program.

Code:
// Find the largest palindrome made from the product of two 3-digit numbers.

using System;
using System.Collections;
using System.Collections.Generic;

namespace Euler4 {
	class MainClass {
		public static void Main (string[] args) {
			const int max = 999;
			
			bool isPalindrome = true;
			long largest = 0;
			List<int > digits = new List<int> ();
			
			for (int x = 1; x <= 999; x++) {
				for (int y = 1; y <= 999; y++) {
					int num = x * y;
					
					while (num > 0) {
						digits.Add (num % 10);
						num = num / 10;
					}
					
					int size = digits.Count ();
					
					if (digits [size - 1] == 0) {
						isPalindrome = false;
						break;
					}
				}
			}
		}
	}
}
I do not understand the two errors I get trying to use the digits.Count function. I'm relearning my basic knowledge of C# and haven't used Lists before, so I've no idea what the warning is. Can anyone here explain to me what the problem is? Thanks for any help given!
__________________
Computer Spec:
AMD 3.0 GHz Quad Core
4 GB DDR3 RAM
320 GB Samsung HDD
GIGABYTE Motherboard
Nvidia GT 430
Crappy Old Dell 15" CRT Monitor
Fedora 15 64 Bit
Reply With Quote
  #2  
Old 19th October 2011, 07:33 PM
Cullin1989 Offline
Registered User
 
Join Date: Jun 2011
Posts: 12
linuxfirefox
Re: C# List Help

Hi.

First off, it wouldn't be so bad if you could include the warnings/errors you're getting. I suppose you are using some IDE? For example from MonoDevelop or Visual Studio you could copy and paste your errors here.

Secondly, the line with digits.Count(); should work just fine. Although you don't really need that line.
You could just write the following condition as
if(digits.Last() ==0)
so you don't get that error, but that is a matter of coding style

Edit1: Ok I tried out your code and here's probablz where the issue is. You are missing a reference to System.Core. If you reference that in your project you will also have access to LINQ (Language Integrated Query) which in turn allows you to use the extension Method .Count() on IEnumerable or some sort. What you can do on the other hand if that is too much of a hassle for you, just stick with the Property .Count (without parentheses) of your List. Of course with that in mind you also cannot use .Last() what I suggested ealrier, because it is yet another of those extension methods.

Last edited by Cullin1989; 19th October 2011 at 07:54 PM.
Reply With Quote
  #3  
Old 19th October 2011, 08:25 PM
FuzzyDoom Offline
Registered User
 
Join Date: Nov 2010
Location: Oklahoma
Age: 19
Posts: 62
linuxchrome
Re: C# List Help

Thanks for the help! I didn't realize I needed to use Linq there, and that did indeed fix the problem. And I went ahead and changed it o digits.Last(). Like I said, still getting use to Lists and their uses.

So now I'm having trouble with just not getting an answer. There's probably a stupid mistake in there that I'm making and not seeing, so I'm hoping someone else can help me scan it. Revised code:

Code:
// Find the largest palindrome made from the product of two 3-digit numbers.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Euler4 {
	class MainClass {
		public static void Main (string[] args) {
			const int max = 999;
			
			bool isPalindrome = false;
			int largest = 0;
			List<int > digits = new List<int> ();
			int num = 0;
			
			for (int x = 100; x <= 999; x++) {
				for (int y = 100; y <= 999; y++) {
					num = x * y;

					
					while (num > 0) {
						digits.Add (num % 10);
						num = num / 10;
					}
					
					if (digits.Last () == 0) {
						isPalindrome = false;
					}
					
					if (digits.Last () == digits.First ()) {
						if (digits [digits.IndexOf (digits.Last ()) - 1] == digits [digits.IndexOf (digits.First ()) + 1]) {
							if (digits.Count () > 5) {
								if (digits [digits.IndexOf (digits.Last ()) - 2] == digits [digits.IndexOf (digits.First ()) + 2]) {
									isPalindrome = true;
								}
							} else
								isPalindrome = true;
						}
					}
			
					if (isPalindrome) {
						largest = num;
					}
				}
			}
			
			Console.WriteLine (largest);
		}
	}
}
Running that I always get 0. I think the problem is where I'm testing it to be a palindrome or not.
__________________
Computer Spec:
AMD 3.0 GHz Quad Core
4 GB DDR3 RAM
320 GB Samsung HDD
GIGABYTE Motherboard
Nvidia GT 430
Crappy Old Dell 15" CRT Monitor
Fedora 15 64 Bit

Last edited by FuzzyDoom; 19th October 2011 at 08:55 PM.
Reply With Quote
  #4  
Old 20th October 2011, 06:53 AM
Cullin1989 Offline
Registered User
 
Join Date: Jun 2011
Posts: 12
linuxfirefox
Re: C# List Help

Good that it is working so far.

I noticed some things in your code and have a suggestion for you. Although my code doesn't yield a viable solution to the problem, I cleaned up some misconceptions:

I moved down the creation of the list a couple lines into your iteration. Because you never cleared it, you were adding 5 digits every iteration thus never getting a positive on your check. And I also changed your check a little bit. Since you are working with a list of integers and these are ValueTypes and IndexOf(x) always returns the first occurence of an item in the list, you would get your digits.Last(something) actually being the second item in the list, since there was your first 0. Hope that clears some confusion. Always gald to help out, so keep asking if you#ve got any more questions.

Code:
// Find the largest palindrome made from the product of two 3-digit numbers.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Euler4 {
	class MainClass {
		public static void Main (string[] args) {
			const int max = 999;
			
			bool isPalindrome = false;
			int largest = 0;
			
			int num = 0;
			
			for (int x = 100; x <= 999; x++) {
				for (int y = 100; y <= 999; y++) {
					int current = num = x * y;
					List<int > digits = new List<int> ();
					
					while (num > 0) {
						digits.Add (num % 10);
						num = num / 10;
					}
					
					if (digits.Last () == 0) {
						isPalindrome = false;
					}
					
					if (digits.Last () == digits.First ()) {
						if (digits [digits.Count() -2] == digits [2]) {
							if (digits.Count () > 5) {
								if (digits [digits.Count() -3] == digits [3]) {
									isPalindrome = true;
								}
							} else
								isPalindrome = true;
						}
					}
			
					if (isPalindrome) {
						largest = current;
					}
				}
			}
			
			Console.WriteLine (largest);
		}
	}
}
Reply With Quote
  #5  
Old 20th October 2011, 07:23 AM
FuzzyDoom Offline
Registered User
 
Join Date: Nov 2010
Location: Oklahoma
Age: 19
Posts: 62
linuxchrome
Re: C# List Help

Thank you yet again! Now I just need to figure out why it's telling me 998001 is the correct answer considering that it's not a palindrome. To Monodevelop, AWAY!

Edit: With a small tweak I'm now getting 900099 as an answer. I'm close, I can feel it. This is why I love programming.

Edit2: It is a problem with the testing for the palindrome part (not that it could hardly be any other part). Putting it into a blank solution with a known palindrome it doesn't seem to acknowledge it.

Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Listtest {
	class MainClass {
		public static void Main (string[] args) {
			List<int > digits = new List<int> ();
			
			digits.Add (1);
			digits.Add (2);
			digits.Add (3);
			digits.Add (2);
			digits.Add (1);
			
			if (digits.Last () == digits.First ()) {
				if (digits [digits.Count () - 1] == digits [1]) {
					if (digits.Count () > 5) {
						if (digits [digits.Count () - 2] == digits [2]) {
							Console.WriteLine (digits [1]); // Just to see output
						}
					} else
						Console.WriteLine (digits [2]); // Just to see output
				}
				
			}
		}
	}
}
I've been staring at it for a while now, and seem to be stumped. Will continue for a bit longer before heading off to bed though.

Edit: Trying a new way now. Using a loop to check for palindrome. Going to bed now, leaving code here for random code-solving super hero to solve. I'm utterly stumped as to why neither one was working. :/

Code:
// Find the largest palindrome made from the product of two 3-digit numbers.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Euler4 {
	class MainClass {
		public static void Main (string[] args) {
			const int max = 999;
			
			bool isPalindrome = false;
			int largest = 0;
			
			int num = 0;
			
			for (int x = 100; x <= max; x++) {
				for (int y = 100; y <= max; y++) {
					int current = num = x * y;
					List<int > digits = new List<int> ();
					
					while (num > 0) {
						digits.Add (num % 10);
						num = num / 10;
					}
					
					int a = 0;
					int b = digits.Count () - 1;
			
					while (a < b) {
						if (digits [a] == digits [b])
							isPalindrome = true;
				
						a++;
						b--;
					}
					
					if (isPalindrome)
						largest = current;
				}
			}
			
			Console.WriteLine (largest);
		}
	}
}
__________________
Computer Spec:
AMD 3.0 GHz Quad Core
4 GB DDR3 RAM
320 GB Samsung HDD
GIGABYTE Motherboard
Nvidia GT 430
Crappy Old Dell 15" CRT Monitor
Fedora 15 64 Bit

Last edited by FuzzyDoom; 20th October 2011 at 08:16 AM.
Reply With Quote
  #6  
Old 20th October 2011, 08:59 AM
Cullin1989 Offline
Registered User
 
Join Date: Jun 2011
Posts: 12
windows_7ie
Re: C# List Help

hehe.
Since I don't want to spoil your adventure, I'm not going to solve your problem yet. Just for inspiration or whatever, here's what I would do first thing coming to my mind. Since I don't have to worry about resources in such a small program, I'm just wasting all I can, but have it pretty straight forward.

I'm curious to see what you come up with, tough.

Code:
// Find the largest palindrome made from the product of two 3-digit numbers.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
 
namespace Euler4
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            const int max = 999;
            int largest = 0;
            for (int x = 100; x <= max; x++)
            {
                for (int y = 100; y <= max; y++)
                {
                    int num = x * y;
                    string forwards = num.ToString();
                    string backwards = new string(num.ToString().ToCharArray().Reverse().ToArray());
                    if (forwards == backwards)
                    {
                        largest = num;
                    }
                }
            }
 
            Console.WriteLine(largest);
            Console.ReadKey();
        }
    }
}
Edit: Or I could just leave it here as a spoiler, if you get stuck down the road.

// Find the largest palindrome made from the product of two 3-digit numbers.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Euler4
{
class MainClass
{
public static void Main(string[] args)
{
const int max = 999;


int largest = 0;

int num = 0;

for (int x = 100; x <= max; x++)
{
for (int y = 100; y <= max; y++)
{
bool isPalindrome = true;
int current = num = x * y;
List<int> digits = new List<int>();

while (num > 0)
{
digits.Add(num % 10);
num = num / 10;
}

int a = 0;
int b = digits.Count() - 1;

while (a < b)
{
if (!(digits[a] == digits[b]))
isPalindrome = false;
if (!isPalindrome)
{
break;
}
a++;
b--;
}

if (isPalindrome)
largest = current;
}
}

Console.WriteLine(largest);
Console.ReadKey();
}
}
}

Last edited by Cullin1989; 20th October 2011 at 09:08 AM. Reason: Spoiler added
Reply With Quote
  #7  
Old 21st October 2011, 07:09 AM
FuzzyDoom Offline
Registered User
 
Join Date: Nov 2010
Location: Oklahoma
Age: 19
Posts: 62
linuxchrome
Re: C# List Help

When looking up ways to do this I've seen plenty uses of people turning it into a string and comparing them, but I decided to stick to ints for fun. And your bottom solution does return a palindrome. Just not the correct one according to Project Euler. Back to testing!

---------- Post added 21st October 2011 at 01:09 AM ---------- Previous post was 20th October 2011 at 05:13 PM ----------

That code gets the largest palindrome from two two digit numbers, but it can't get the largest from two three digit numbers. I wonder what the problem is...

EDIT: I FIGURED OUT THE DAMN PROBLEM! HA! I made it print out the numbers that it was testing and all the answers were palindromes. I looked up the answer online and found it was 906609, which you get 2 palindromes before 580085 (the answer it gave you.) I forgot to put testing to see if the new palindrome was larger than the one before it. I had just assumed that the last number would end up with the largest palindrome. That's a bit of logic fail right there. Thank you for all your help Cullin!

Code that finally solved the problem:

Code:
// Find the largest palindrome made from the product of two 3-digit numbers.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Euler4 {
	class MainClass {
		public static void Main (string[] args) {
			const int max = 999;

			int largest = 0;

			int num = 0;

			for (int x = 100; x <= max; x++) {
				for (int y = 100; y <= max; y++) {
					bool isPalindrome = true;
					int current = num = x * y;
					List<int > digits = new List<int> ();

					while (num > 0) {
						digits.Add (num % 10);
						num = num / 10;
					}

					int a = 0;
					int b = digits.Count () - 1;

					while (a < b) {
						if (!(digits [a] == digits [b]))
							isPalindrome = false;
						
						if (!isPalindrome)
							break;
						
						a++;
						b--;
					}

					if (isPalindrome) {
						if (current > largest)
							largest = current;
					}
				}
			}
			
			Console.WriteLine (largest);
		}
	}
}
__________________
Computer Spec:
AMD 3.0 GHz Quad Core
4 GB DDR3 RAM
320 GB Samsung HDD
GIGABYTE Motherboard
Nvidia GT 430
Crappy Old Dell 15" CRT Monitor
Fedora 15 64 Bit

Last edited by FuzzyDoom; 21st October 2011 at 07:24 AM.
Reply With Quote
  #8  
Old 21st October 2011, 10:28 AM
Cullin1989 Offline
Registered User
 
Join Date: Jun 2011
Posts: 12
windows_7ie
Re: C# List Help

Good that you solved your problem. I'm also glad I could be of help. Yes it's kind of a trivial error that I also let slip by me

Oh well. Until next time, I guess. Also feel free to pm me if you've got a question concerning C#, .Net, Mono and the likes.
Reply With Quote
Reply

Tags
list

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
repository list and list of installed applications? dragonbite Using Fedora 10 30th November 2011 11:40 AM


Current GMT-time: 10:31 (Thursday, 20-06-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat