HardQuor's forum posts

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

23

Followers

Reviews: 2

User Lists: 0

#1 HardQuor
Member since 2007 • 1282 Posts
Well, problem 3 seems to be a lot simpler than i thought it would be. It asks for the largest prime number of 600 billion something. I've got the code to work on a smaller number, but unfortunately, i've run into the problem of my int data type not being able to contain the full 12 digits of the term. I've tried long ints, unsigned ints, and unsigned long ints. Anyone have a solution?
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

23

Followers

Reviews: 2

User Lists: 0

#2 HardQuor
Member since 2007 • 1282 Posts

I thought about trying the Project Euler thing when you brought it up.

On the one hand, I like the programming part. If I didn't, then I put a lot of effort into getting a degree in something I hate (and got near-straight A's in it, too... bloody interface design :evil: )... :lol:

On the other hand, I don't do well with numbers (something that might baffle other programmers, and my math teachers in college). It took me several tries to finally understand algebra (high school, two math courses in the Navy, then it finally clicked in college... where I tutored math, too... :? ), and arithmetic can get me in trouble (9 * 7 != 16... I know it, I feel it, but it's even money if I actually write it correctly).

I'll ponder some more... I'm leaning towards "go for it," though. ;)

OrkHammer007
I think you should totally go for it, my math skills are quite lacking as well. Hell, i was assigned high school algebra when i took my college placement tests. Besides, the more the merrier :D

[QUOTE="HardQuor"]Yeah, that's way over my head :( I'll just consider my solution a good compromise between conciseness and elegance :PGabuEx

Well, it's actually not too conceptually difficult.  I probably just explained it badly.  There are two main concepts that need to be understood with what I was talking about:

1. First, there are prime numbers (numbers which are divisible by only 1 and itself), and there are composite numbers (numbers which are divisible by more than that).  Any composite number can be represented as a product of prime numbers (for example, 12 = 2 * 2 * 3).

2. Second, to say that x is divisible by y is to say that all prime factors of y are also prime factors of x.  For example, 12 is divisible by 4 because 12 = 2 * 2 * 3, while 4 = 2 * 2 - all prime factors of 4 are also prime factors of 12.

See if what I said makes any more sense in light of those two things (or tell me if the above also makes no sense).

No, i think you explained prime factors just fine (which i'm thankful for, as the next problem for Project Euler deals directly with figuring them out for a ridiculously large number). What is over my head is conceptualizing it all in code. Although, i'm not really dedicating a lot of thought to it, admittedly :P
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

23

Followers

Reviews: 2

User Lists: 0

#3 HardQuor
Member since 2007 • 1282 Posts

Well, yours was a bit better, but there's actually a very much more streamlined way of doing this.  If you're curious about what that is,

...

This requires code that's rather more complicated than what we've got here, though.  Just for fun, though, I implemented it as a proof of concept; it pretty much instantly finds the answer.

 

GabuEx
Yeah, that's way over my head :( I'll just consider my solution a good compromise between conciseness and elegance :P
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

23

Followers

Reviews: 2

User Lists: 0

#4 HardQuor
Member since 2007 • 1282 Posts
...I'm still trying to figure out why that worked... OrkHammer007
I'm glad you left me an opening to explain my methodology, i'm a little proud of it (more than i should be, anyway :P). The reason is because i approached it more like a human approaches similar problems. When we try to find lowest common denominators, we start from the highest number and work our way down. Just think about breaking change, you usually start with quarters first, then dimes, nickels, and finally pennies. So instead of incrementing n, i decided to decrement it.

Then i had the problem of making x divisible by each previous term as well as the one it was in the loop for. I figured my problem was that i was incrementing x by 1, which is obviously not divisible by any number but 1. so since i was decrementing n anyways, it made sense to just increment x by it's previous value, since those are the smallest increments that are divisible by each previous value of n.
I'm a bit dense at the moment: programming challenges tend to cost me sleep (one time, while I was taking C++ 1 in college, I was at the PC for 12 straight hours because I literally couldn't sleep until I figured out the method that output a pyramid on the screen).

 

OrkHammer007
haha, I've experienced similar frustrations with sleep since i've started Project Euler :P

Don't worry about annoying me: I tutored programming (VB, C++, and Java) to pay part of my tuition, and I love a good coding challenge (even if it aggravates my insomnia :lol: ). Ask away!

OrkHammer007
that's good news! :) originally, i had thought to get more responses from others around the boards, but since it's just basically three of us, i wanted to make sure it was cool with you and gabu if i kept pestering you :P

also, seeing as you're such a big fan of these challenges, are you interested in checking out Project Euler yourself?

Oh... and I think GS is running part of your code: somehow, you quadruple-posted. :shock:

OrkHammer007
haha, yeah, i thought i was at the "edit post" screen, not the "new post" screen :P
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

23

Followers

Reviews: 2

User Lists: 0

#5 HardQuor
Member since 2007 • 1282 Posts

[QUOTE="HardQuor"]since (0 % 0) == 0GabuEx

Nope. x % 0 for any x is asking "what is the remainder when x is divided by 0"? Division by 0 is obviously not allowed, which thus causes your program to crash.

Ahh. Now i get it. I feel dense :(

Actually, there's an even easier way to do this that doesn't need two loops or extra variables:

    int x = 1;

    for (int n = 2; n (less than or equal to) 20; n++)
    {
         if (x % n != 0)
         {
                x++;
                n = 1;
         }
    }

GabuEx
I should've known better than to think that my code was optimal, you just blew my mind!:shock::lol:

This basically loops through all values of n, and if it finds that x is not divisible by one of the values, then it increments x by 1 and starts over.

Of course, this is very brute force, and there is a very elegant way of figuring it out if you add in a bit of smart math, but this nonetheless works.

GabuEx
was that? could it be? did you complement my code, or am i delusional? :lol:
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

23

Followers

Reviews: 2

User Lists: 0

#6 HardQuor
Member since 2007 • 1282 Posts

Hopefully, you can read the code from the screen shot; since the forum code doesn't like C++/Java, it'll probably work a bit better like this.

It's written in Java (I'm a bit more comfortable with it), but it should be simple to convert it to C++.

It took about 25 seconds to run, and it came up with a figure in 9 digits... :shock:

Hopefully, this helps. :D

OrkHammer007

oops, i didn't get to see your post before i decided to see my methodology to the end :?

but i do have good news, i got it!

    int x=20;
    int y=0;
    for( int n=20; n > 1; n-- )
    {
         while( (x % n) != 0 )
         {
                x = x + y;
         };
         y = x;
    };

I figured that my mistake would be easily corrected by just incrementing x by it's last known value instead of by 1. And even better, the code is simpler and it rendered instantly :) thanks for your help and sorry i wasn't able to consider your contribution :?

By the by, I hope i'm not annoying you guys with my constant questions? :?

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

23

Followers

Reviews: 2

User Lists: 0

#10 HardQuor
Member since 2007 • 1282 Posts
Dude, I've slept with like 8 twelve-year-olds[spoiler] When I was twelve! :lol: [/spoiler]