Computer Science Canada Project Euler |
Author: | Martin [ Sun May 21, 2006 9:18 pm ] |
Post subject: | Project Euler |
http://www.mathschallenge.net/index.php?section=project Fun and pretty straightforward programming contest. Check it out. |
Author: | Cervantes [ Sun May 21, 2006 9:34 pm ] |
Post subject: | |
With credit for the link going to bugzpodder, of course. ![]() |
Author: | Martin [ Sun May 21, 2006 9:51 pm ] |
Post subject: | |
I should start reading the contests forum I think ![]() I found it through some Ruby site while looking for stuff on Ruby coding style. Anyway, locked. |
Author: | zylum [ Wed May 24, 2006 7:27 pm ] |
Post subject: | |
why did you lock it? the thread bugz posted the link in was about something else... anyway, how many problems have you solved? lets discuss some of the problems here ![]() i have done problems 1-11,13,16 and 20.. |
Author: | Martin [ Wed May 24, 2006 11:03 pm ] |
Post subject: | |
Yeah, I locked it before I looked up bugz's post, then forgot about it :p So far I've done 1-25, 28, 37, 40, 42, 48 and 79. EDIT: Here's my profile. |
Author: | zylum [ Thu May 25, 2006 5:31 pm ] |
Post subject: | |
now i did 1-25 (except 19) and 67... can someone just tell me the answer to 19? that question is annoying ![]() |
Author: | Martin [ Thu May 25, 2006 6:23 pm ] |
Post subject: | |
19 is really easy. The Jan 1 1901 is a Monday. January has 31 days. What day of the week will it be 31 days after a Monday? nextday = (today + 31) mod 7. After that, February has 28 days (because it's not a leap year). So nextday = (today + 28) mod 7. And so on. |
Author: | zylum [ Thu May 25, 2006 11:29 pm ] |
Post subject: | |
yeah, easy but tedious... my least favourite one thus far was the one where you had to count how many letters it would take to spell out all the numbers from one to one thousand ![]() btw, jan 1st, 1900 was no necessarily a monday, it says jan 1900.. unless you have found this out then... actually according to http://www.timeanddate.com/calendar/index.html?year=1901&country=1 jan 1, 1901 is a Tuesday ![]() [edit] yay i got it ![]() |
Author: | Bobrobyn [ Fri May 26, 2006 7:32 am ] |
Post subject: | |
zylum wrote: yeah, easy but tedious... my least favourite one thus far was the one where you had to count how many letters it would take to spell out all the numbers from one to one thousand
![]() btw, jan 1st, 1900 was no necessarily a monday, it says jan 1900.. unless you have found this out then... actually according to http://www.timeanddate.com/calendar/index.html?year=1901&country=1 jan 1, 1901 is a Tuesday ![]() [edit] yay i got it ![]() If you enjoy solving the problems, it really isn't tedious. Anyways, thanks for the link. I'll try it sometime -- probably after exams. |
Author: | zylum [ Fri May 26, 2006 12:56 pm ] |
Post subject: | |
hmm.. i just realized something... in my program i didnt check for leap years and i still got the correct answer ![]() |
Author: | zylum [ Mon May 29, 2006 12:06 am ] |
Post subject: | |
528 points, 44 problems solved ![]() solved all problems of difficulty < 16 except for 45... cant seem to get that one to work.. any ideas? |
Author: | Martin [ Mon May 29, 2006 2:04 am ] |
Post subject: | |
zylum wrote: 528 points, 44 problems solved
![]() Oh it's on Zylum. |
Author: | zylum [ Mon May 29, 2006 7:10 pm ] |
Post subject: | |
Martin wrote: zylum wrote: 528 points, 44 problems solved
![]() Oh it's on Zylum. oh is it? http://mathschallenge.net/index.php?section=project&ref=scores&profile=4547 |
Author: | bugzpodder [ Fri Jun 09, 2006 8:49 am ] |
Post subject: | |
wow u guys are fast. i probably dont even as many solved as martin does (about the same) |
Author: | bugzpodder [ Sat Jun 10, 2006 10:08 am ] |
Post subject: | |
really do you guys do this 12 hours a day? now you guys are like 700 points, way ahead of me ![]() |
Author: | Null [ Sat Jun 10, 2006 10:46 am ] |
Post subject: | |
With grade 10 (enriched) math, I've solved: 1-8, 13, 16, 20, 22, 25, and 48. (mostly using Ruby, but with a bit of C++ when there was heavy number crunching). For a few of the problems that I haven't solved, the program would theoretically work, but takes too long to calculate. |
Author: | Martin [ Sun Jun 11, 2006 7:08 pm ] |
Post subject: | |
bugzpodder wrote: really do you guys do this 12 hours a day? now you guys are like 700 points, way ahead of me
![]() Nah, first bunch I did in a 3 or so hour stretch, lately I've been trying to do one question every couple of days. I'm kind of stuck though - there are a few more that I'll be able to get, but after that I don't even know where to begin. |
Author: | cool dude [ Sun Jun 11, 2006 8:58 pm ] | ||
Post subject: | |||
i just started solving some of them and problem 2 is bugging me! i am pretty sure the answer is: 24196302. although it says its incorrect. the question was: Quote: Problem 2 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Find the sum of all the even terms in the sequence below one million. i used java to try and solve this question and this is wat i have which theoretically is correct from wat i understood from the instructions.
|
Author: | wtd [ Sun Jun 11, 2006 10:47 pm ] |
Post subject: | |
Does your code account for "num[i]" being equal to 2? |
Author: | zylum [ Mon Jun 12, 2006 12:04 am ] |
Post subject: | |
all terms below one million. not the first one million terms. ![]() i dont do this anymore that often. i probably spent about 10 hours in total on all of these. as martin said, the first bunch dont take too long. for the sudoku one i just used a program that i already made (which is posted somewhere on this site). |
Author: | bugzpodder [ Mon Jun 12, 2006 8:49 am ] |
Post subject: | |
Martin wrote: bugzpodder wrote: really do you guys do this 12 hours a day? now you guys are like 700 points, way ahead of me
![]() Nah, first bunch I did in a 3 or so hour stretch, lately I've been trying to do one question every couple of days. I'm kind of stuck though - there are a few more that I'll be able to get, but after that I don't even know where to begin. How come you are stuck?? is it because it is hard to make efficient code? |
Author: | cool dude [ Mon Jun 12, 2006 4:51 pm ] |
Post subject: | |
[quote="zylum"]all terms below one million. not the first one million terms. ![]() quote] Thanks zylum. i hate play on word puzzles. no wonder i did bad in english lol. |
Author: | Martin [ Mon Jun 12, 2006 8:59 pm ] |
Post subject: | |
bugzpodder wrote: Martin wrote: bugzpodder wrote: really do you guys do this 12 hours a day? now you guys are like 700 points, way ahead of me
![]() Nah, first bunch I did in a 3 or so hour stretch, lately I've been trying to do one question every couple of days. I'm kind of stuck though - there are a few more that I'll be able to get, but after that I don't even know where to begin. How come you are stuck?? is it because it is hard to make efficient code? For some of them. The big problem though is that I don't know the algorithms to actually do what I want to (like, even if everything would execute instantly I wouldn't know where to start). Right now I'm trying to figure out the recurring decimal problems, they're kind of tricky. Slowly hacking through Introduction to Algorithms too. I'll figure it out eventually. |
Author: | zylum [ Tue Jun 13, 2006 8:36 am ] |
Post subject: | |
the decimal problem involves primes.. not just any primes. they form a sequence. find the largest prime in that sequence such that it is less than 1000.. there, you dont even need a program ![]() i find that a lot of these problems involve some sort of math related trivia. you need some sort of formula or something otherwise it wont execute fast enough. i usually do a quick search in google before i write any code. also search mathworld.wolfram.com and the online sequence encyclopedia |
Author: | cool dude [ Tue Jun 13, 2006 7:35 pm ] |
Post subject: | |
how did u do problem 20. i'm getting a really really large number and i did the problem correctly (or at least how i understood it). i prolly misunderstood it like the other one so any hints? |
Author: | Martin [ Tue Jun 13, 2006 8:48 pm ] |
Post subject: | |
It's about as straightforward as they come. Compute 100! Find the sum of the digits. |
Author: | cool dude [ Tue Jun 13, 2006 10:06 pm ] | ||
Post subject: | |||
i might be wrong but try punching that in on your calculator the number is so big that it gives u an error and if u were to add up all the numbers up it would be even bigger.
|
Author: | zylum [ Tue Jun 13, 2006 10:08 pm ] |
Post subject: | |
java = BigInteger ruby = does it on its own.. Martin uses ruby ![]() oh, and your computing the sum of the digits incorrectly.. say the anser is 23164 then the sum of the digits is 2+3+1+6+4 = 16 |
Author: | cool dude [ Tue Jun 13, 2006 10:24 pm ] |
Post subject: | |
see once again i misunderstood the instructions, and thanks again zylum. i thought u had to add up all the digits from 1 to that number. i didn't know u had to add the digits of the actual number. now i need to go learn BigInteger. |
Author: | zylum [ Tue Jun 13, 2006 10:53 pm ] |
Post subject: | |
a lot of the problems need the knowledge of BigInteger so it wont be a waste of time learning it. |
Author: | wtd [ Wed Jun 14, 2006 5:18 pm ] | ||
Post subject: | |||
Scheme also automatically handles arbitrary precision math.
|
Author: | Clayton [ Wed Jun 14, 2006 5:39 pm ] |
Post subject: | |
Martin wrote: It's about as straightforward as they come.
Compute 100! Find the sum of the digits. i though x! was a factorial eg. (x*x-1*x-2....*x-(x-1)) if its not then what is it? |
Author: | bugzpodder [ Wed Jun 14, 2006 6:09 pm ] | ||
Post subject: | |||
wtd wrote: Scheme also automatically handles arbitrary precision math.
hey hey hey! I understand this!! ![]() |
Author: | wtd [ Wed Jun 14, 2006 6:39 pm ] |
Post subject: | |
Excellent! |
Author: | nate [ Tue Jun 20, 2006 5:17 pm ] |
Post subject: | |
What is the sum of the digits of the number 2^1000? I was wondering if anyone could help me out with this problem. I have no clue how I would be able to calculate 2^1000 and find its digits cause its just such a big number 302 digits i think. I'm using Turing (that may be my problem to as I don't see turing as one of the methods ppl used to solve these). |
Author: | GlobeTrotter [ Tue Jun 20, 2006 10:16 pm ] |
Post subject: | |
I really am not so sure it can be done in turing. The obvious way to go about that problem is to convert the number to a string and then make functions like add, multiply, etc onto that string (using grade 3 type math). The problem with turing is that, even the string type is limited. If my memory serves me well, it can only habdle 255 characters, so a 300-digit number may be undoable. On second thought, if you convert to something other than base 10 (eg. base 64) it may be doable, I'm not sure. |
Author: | zylum [ Wed Jun 21, 2006 3:51 pm ] |
Post subject: | |
java BigInteger class.. and sure it can be done in turing.. just use arrays. |
Author: | wtd [ Wed Jun 21, 2006 8:29 pm ] | ||||
Post subject: | |||||
zylum wrote: java BigInteger class..
Or just use something that automatically does this. ![]() Scheme:
Common Lisp:
|
Author: | Null [ Thu Jun 22, 2006 10:14 am ] | ||
Post subject: | |||
Or Ruby ![]()
|
Author: | cool dude [ Sat Jul 01, 2006 1:34 pm ] |
Post subject: | |
how would i make a program that gets the sum of 100 - 50 digit number? wat i mean is if i had 50 numbers on one line and then having 100 lines. so in total there would be 15,000 digits. this is problem 13 in the math challenge. |
Author: | wtd [ Sat Jul 01, 2006 3:46 pm ] |
Post subject: | |
cool dude wrote: how would i make a program that gets the sum of 100 - 50 digit number?
wat i mean is if i had 50 numbers on one line and then having 100 lines. so in total there would be 15,000 digits. this is problem 13 in the math challenge. Let me answer that with a question. Do you actually care about the value of that 100 digit number? |
Author: | cool dude [ Sun Jul 02, 2006 10:06 am ] |
Post subject: | |
wtd wrote: cool dude wrote: how would i make a program that gets the sum of 100 - 50 digit number?
wat i mean is if i had 50 numbers on one line and then having 100 lines. so in total there would be 15,000 digits. this is problem 13 in the math challenge. Let me answer that with a question. Do you actually care about the value of that 100 digit number? i don't care about the value of the whole number, rather i care about each digit individually. but if i use substring for java to get each individual number the whole number will be too long to even look at. 15,000 digits! |
Author: | wtd [ Sun Jul 02, 2006 11:42 am ] |
Post subject: | |
If you're using substring to retrieve a single character, then you haven't read the String class docs thoroughly. |
Author: | cool dude [ Sun Jul 02, 2006 1:26 pm ] |
Post subject: | |
wtd wrote: If you're using substring to retrieve a single character, then you haven't read the String class docs thoroughly.
why can't u do that? u make the number a string take each digit seperately, then convert each digit to an integer and add them to get the sum. if the number was only 10 digits long instead of 15,000 i would be able to do that. |
Author: | wtd [ Sun Jul 02, 2006 1:31 pm ] |
Post subject: | |
I never said you shouldn't use a String. You just shouldn't use substring to get at individual characters in a String. |
Author: | zylum [ Sun Jul 02, 2006 5:13 pm ] |
Post subject: | |
to get the numerical value of a digit in a string, you simply do: someString.charAt(i) - '0' which gives you the value of the number at position 'i' in the string someString. this works because the unicode values of the digits 0-9 are in order. so if you subtract the unicode value for '0' by '0' you get 0. '1' - '0' = 1 and so on. |
Author: | cool dude [ Sun Jul 02, 2006 6:18 pm ] |
Post subject: | |
k now my problem is when i copied and pasted the number; the number goes on to next lines. so how do i make it read as one number. it gives me an error saying unclosed string literal. |
Author: | wtd [ Sun Jul 02, 2006 6:24 pm ] |
Post subject: | |
Express it as two literal strings. Now, do you know how to make two literal strings into one string? |
Author: | cool dude [ Sun Jul 02, 2006 6:38 pm ] |
Post subject: | |
wtd wrote: Express it as two literal strings. Now, do you know how to make two literal strings into one string?
wouldn't you just put a + at the end of each string? |
Author: | wtd [ Sun Jul 02, 2006 6:39 pm ] |
Post subject: | |
Between the strings, yes. |
Author: | cool dude [ Sun Jul 02, 2006 6:42 pm ] |
Post subject: | |
one problem. i think the charAt is screwed up because when i'm getting the value of each number, i don't think its reading the next string as a continuation, so i'm getting wrong values. the reasoning to my thinking is because i'm getting the wrong sum of all the numbers. |
Author: | wtd [ Sun Jul 02, 2006 6:45 pm ] |
Post subject: | |
Perhaps you could post your code? |
Author: | cool dude [ Sun Jul 02, 2006 6:48 pm ] | ||
Post subject: | |||
|
Author: | wtd [ Sun Jul 02, 2006 6:51 pm ] |
Post subject: | |
Two things: 1) How do you know the string is exactly 5000 characters long? Perhaps you should use the length() method of the String class to figure this out. 2) Do you understand what charAt returns, and how characters and integers are related? Hint: zyllum gave you the answer. |
Author: | cool dude [ Sun Jul 02, 2006 7:05 pm ] |
Post subject: | |
i know that it is 5000 digits long because 100 * 50 = 5000. but true i should be using the length() method. i don't think i understand exactly how characters and integers are related and i'm a little confused on zylums explanation. ![]() |
Author: | wtd [ Sun Jul 02, 2006 7:13 pm ] |
Post subject: | |
In Java, characters are represented as 16-bit integers. The digits 0-9 are represented as consecutive integers. |
Author: | cool dude [ Mon Jul 03, 2006 10:02 am ] |
Post subject: | |
so how would i convert it? |
Author: | cool dude [ Mon Jul 03, 2006 4:40 pm ] |
Post subject: | |
can u please explain 16-bit integers. |
Author: | cool dude [ Tue Jul 04, 2006 7:37 pm ] | ||
Post subject: | |||
is there a better way of doing problem 12 in the challenge, because my code is taking forever to run. can i optimize my code somehow to make it go faster?
the problem was: Quote: The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the first seven triangle numbers: 1: 1 3: 1,3 6: 1,2,3,6 10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 We can see that the 7th triangle number, 28, is the first triangle number to have over five divisors. Which is the first triangle number to have over five-hundred divisors? |
Author: | zylum [ Tue Jul 04, 2006 8:25 pm ] |
Post subject: | |
read the faq on the site. it has some useful formulas such as how to calculate the nth triangular number and how to quickly find how many divisors a number has. |
Author: | cool dude [ Tue Jul 04, 2006 10:17 pm ] |
Post subject: | |
i read How many divisors does a number have? but i don't really understand there explanation. they gave an example: Quote: d(48)=5×2=10.
where did they get 5 * 2 from? ![]() |
Author: | Hikaru79 [ Wed Jul 05, 2006 12:33 am ] | ||||||||||
Post subject: | |||||||||||
It's a combinatorics question. Let's break up 48 into it's prime factors.
In other words,
Now, you can think of a divisor as a number whose factors are a subset of the factors of the original number. So how many subsets can you make when you have four A's and 1 B? You can choose to have either 0,1,2,3 or 4 factors of 2 (5 choices) and either 0 or 1 factor of 3 (2 choices). Multiplying together, we have
Do you see? Note: The general rule, basically, is that if:
Basically all you do is prime factor the number, take all the exponents, add 1 to each of them, and multiply them. |
Author: | cool dude [ Wed Jul 05, 2006 10:34 am ] |
Post subject: | |
thanks for the explanation i get it ![]() |
Author: | Cervantes [ Wed Jul 05, 2006 11:28 am ] | ||
Post subject: | |||
Create an array of primes. Depending on how you calculate the next prime number after a given prime, this array might not be necessary.
You only need the first prime number to start with. If your number n is divisible by 2, then make n equal to n / 2, add 2 to your list of prime factors, and check once again if (the new) n is divisible by 2. If n is not divisible by 2, calculate the next prime number (can be done a variety of ways), and move back to step 1, except using this next prime number instead of 2. Repeat this process until n is prime (which is the same as saying until the prime number you are checking is bigger than the squareroot of the original n). |
Author: | cool dude [ Wed Jul 05, 2006 11:50 am ] |
Post subject: | |
Cervantes wrote: calculate the next prime number (can be done a variety of ways)
Repeat this process until n is prime (which is the same as saying until the prime number you are checking is bigger than the squareroot of the original n). two things: 1)the only way i can think of calculating the next prime number would be to use a for loop to go through all the numbers until the original prime number and see if any of them are divisible equally. i.e. [code] if (prime % i == 0). [code] However this is not efficient because when i get into really large numbers it takes a very long time. is there any other way to calculate the next prime numbers? 2) you said to repeat the process until the prime number is bigger that the square root of the original, but wat if the original is not a perfect square, thus will be a decimal number. how would i compare them?[/code] |
Author: | Hikaru79 [ Wed Jul 05, 2006 8:28 pm ] |
Post subject: | |
To calculate the prime numbers up to some value n, a common and fairly simple technique is called the "Sieve of Eratosthenes". Google that up and you'll find tons of explanations ![]() |
Author: | cool dude [ Wed Jul 05, 2006 8:42 pm ] |
Post subject: | |
Hikaru79 wrote: To calculate the prime numbers up to some value n, a common and fairly simple technique is called the "Sieve of Eratosthenes". Google that up and you'll find tons of explanations
![]() i don't need the prime numbers i need all the numbers that can be divided into a certain number. for example, 10 the divisors are 1,2,5,10. there are 4 divisors. now wat i need is to figure out a number that has 500 divisors. so i don't only want to calculate the prime numbers i want all the numbers that are divisible into a certain number. ***Edit*** Unless you meant to say prime factors |
Author: | Cervantes [ Wed Jul 05, 2006 8:53 pm ] |
Post subject: | |
cool dude wrote: now wat i need is to figure out a number that has 500 divisors. so i don't only want to calculate the prime numbers i want all the numbers that are divisible into a certain number.
|
Author: | the_short1 [ Sat Jul 08, 2006 8:24 pm ] |
Post subject: | |
Wow, this is fun stuff.. thanks for the link... so far i did 1-3,5,9 then i started 54 cuz it looked like fun.. but i just started this about an hour ago.. zylum your nuts.. .. [to Cervantes... do you have a list of primes somewhere ? ![]() |
Author: | cool dude [ Sun Jul 09, 2006 10:53 am ] |
Post subject: | |
the_short1 wrote: Wow, this is fun stuff.. thanks for the link... so far i did 1-3,5,9 then i started 54 cuz it looked like fun.. but i just started this about an hour ago.. zylum your nuts.. ..
[to Cervantes... do you have a list of primes somewhere ? ![]() ha to make a program display all the primes from 1 to watever number you want takes no time! if you're that lazy i believe wikipedia has a list of a lot of prime numbers. the first couple problems take very little time, but then they get harder and harder, so now i'm kinda stuck on several, because i'm learning a new language java and at the same time trying to solve the problems in the same language, which limits me because i don't know much. |
Author: | Cervantes [ Sun Jul 09, 2006 12:15 pm ] |
Post subject: | |
the_short1 wrote: [to Cervantes... do you have a list of primes somewhere ?
![]() There are such lists all over the internet. But, have you no scruples? Using pre-generated lists is cheating. |
Author: | the_short1 [ Sun Jul 09, 2006 3:47 pm ] |
Post subject: | |
Cervantes wrote: the_short1 wrote: [to Cervantes... do you have a list of primes somewhere ?
![]() There are such lists all over the internet. But, have you no scruples? Using pre-generated lists is cheating. well that still doesnt solve the problem that it asks [so not cheating imo].. . but yea i just finished making a primes program so ill finish all those primes questions later today .. ![]() Edit: the question i was refering to wasnt the 1001st prime, it was the turncating primes from left and right.... |
Author: | Cervantes [ Mon Jul 10, 2006 7:37 am ] |
Post subject: | |
With most of the primes questions I've seen so far, a primes list pretty much solves it. Like, "what is the 10001'st prime"? I would also consider it cheating to pregenerate a list of primes yourself. You could let that run for hours and then use that list to solve problems, but then each of those problems really had a run time of hours, when it should run within a single minute. |
Author: | the_short1 [ Mon Jul 10, 2006 10:43 pm ] |
Post subject: | |
yea.. i went about programming the list of primes wrong at first, and it was taking too long so thats why i wanted to find a list.. i run my new primes method with every question so it factors into the time and its <1minute.. my bad, plz forgive my lazyness ![]() so far i finished 15questions and got 125 pts... but my "q45" is taking foreever to execute /w java.. i have 3 for loops first one goes thru triangle numbers and two interior ones to go through pent and hex, it worked to find the 40577 one in like 2 minutes.. but its been toasting for a good 30minutes now without finding another one.. each for loop goes to 100k, and im using bigInteger for the result of T. P. H. equations......any ideas? any rough guess on how big those ranges should be (rather then 100k).... or did you manage to use a double/float ? (it was messing me up so i switched to big) thanks for any help |
Author: | zylum [ Tue Jul 11, 2006 12:27 am ] |
Post subject: | |
hint: you dont need to loop through triangular, pentagonal and hexagonal numbers. you only need to loop through hexagonal numbers. (all triangular numbers are hexagonal) |
Author: | the_short1 [ Tue Jul 11, 2006 6:30 pm ] |
Post subject: | |
zylum wrote: hint: you dont need to loop through triangular, pentagonal and hexagonal numbers. you only need to loop through hexagonal numbers. (all triangular numbers are hexagonal)
Thanks Zylum ![]() ![]() |
Author: | cool dude [ Wed Jul 12, 2006 1:16 pm ] |
Post subject: | |
i'm using 3 for loops and its taking forever. how did u manage to do it without the 3 for loops? |
Author: | the_short1 [ Wed Jul 12, 2006 9:36 pm ] |
Post subject: | |
cool dude wrote: i'm using 3 for loops and its taking forever. how did u manage to do it without the 3 for loops?
I hope this doesn't spoil the question, but as zylum mentioned, you only have to loop through hexagonal numbers, then test to see if that number is also pentagonal. The tricky part is.. the number you get back from the hexagonal formula is the result you should get from the pentagonal.. not the pentagonal term...so say your for loop has a value of 3... Hex3 = 15 = PentX, solve for X (and is that value for X a valid pentagonal value ?) in this case there is no value for X to make a pentagonal number of 15! Also, that still doesnt give you the Triangle term..more math..but at least you dont need to check validity of that one .. ![]() |
Author: | [Gandalf] [ Thu Jul 13, 2006 1:58 am ] |
Post subject: | |
Is anyone still doing this other than Minsc and the_short1? Anyway, I joined and solved 10 problems today. ![]() Fun stuff, and I must say slightly addicting... Or maybe that's just me. ![]() |
Author: | Cervantes [ Thu Jul 13, 2006 7:31 am ] |
Post subject: | |
[Gandalf] wrote: Is anyone still doing this other than Minsc and the_short1?
Yeah, I'm doing it. ![]() |
Author: | cool dude [ Thu Jul 13, 2006 10:54 am ] |
Post subject: | |
i think i'm terrible at algebra, but can someone help me out. i hadn't had algebra for a long time so can't remember. n(3n-1)/2 = hex (Let's say hex = 15) solve for n on its own. i don't need wat n equals. instead i need an equation where n is on one side and everything else is on the other side. |
Author: | Cervantes [ Thu Jul 13, 2006 11:29 am ] |
Post subject: | |
You can't isolate n completely there. Rather, you expand that expression on the left, bring the 15 from the right over to the left so that the left expression equals to zero, and solve your quadratic. You know how to solve a quadratic, right? |
Author: | cool dude [ Thu Jul 13, 2006 12:05 pm ] |
Post subject: | |
Cervantes wrote: You can't isolate n completely there. Rather, you expand that expression on the left, bring the 15 from the right over to the left so that the left expression equals to zero, and solve your quadratic.
You know how to solve a quadratic, right? thanks. i was about to delete that post right now because i just figured out that i can't completely isolate for n and have to use the quadratic formula. i just thought there was some other way, but apperently not. thanks anyways. ![]() |
Author: | Martin [ Thu Jul 13, 2006 7:52 pm ] |
Post subject: | |
I'll get back to it this weekend I think. |
Author: | cool dude [ Thu Jul 27, 2006 1:26 pm ] | ||
Post subject: | |||
not sure if you guys are still doing this but for problem 14 do you have any hints where i can optimize my code because its taking hours to run this program
|
Author: | Cervantes [ Thu Jul 27, 2006 4:18 pm ] |
Post subject: | |
That's a basic brute force, it seems. Try thinking about another, more clever way to do it. Perhaps you should look at a few series, for example. Say we start with 13, as they give as an example. The sequence is: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 Now, let's say we're running your code. Okay, we ran through the sequence starting with 13. We try 14. 15. 16. La di da di da. We make our way up and say now we're trying 26. The sequence for 26 is: 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 Is there really any point to going to calculate the sequence beyond 13, since we already did that? No. All we care about is that the sequence once we get to 13 is ten terms long. So starting at 26, we must have a 10+1 = 11 term long sequence. |
Author: | the_short1 [ Thu Jul 27, 2006 5:47 pm ] |
Post subject: | |
Im curious if anyone has finished question 97 I tried with biginteger.pow.. and it wasnt getting anywhere.. ..and not gona finish in the next 2 days (i calculated it using mersenne.org's benchmarks).. hints? as well, i tried question 113.. "How many numbers below a googol (10^100) are not bouncy?" It works to produce the right ammount for up to 1mill, but that already takes far too long.. I do some preemptive checking which does speed it up, as in if there is two of the same digit with a space, then its bouncy.. ie: 1828.. if it has zeros that arent at the end...and 5 random spot checks of 3 consecutive digits... (if digit a<-b and b->c) or (if a->b and b<-c) then it skips past checking the rest... since that would make it neither increasing or decreasing.. But there must be a better way to verify decreasing/increasing then going through the length of the entire string seing if currentvalue is > lastvalue thanks.. -Kevin P.S. Zylum, im gona give you a run for your money unless you start doing more questions... im only ~20 points under you ![]() Edit: Cool Dude: Your problem is not efficientcy.. its the limits for your variable type... my program is almost the same as yours and it runs within the minute... but at first it took hours.. because it gets stuck on one number around 115k that as it progresses down the chain gets over the max for int's....goes into negative, then endless loop...your variable "n" has to be of type "long".. problem fixed. Hint: when a program isnt working like you think it should, add a System.out.println with the variables that are changing, and trace it to the problem. thats how i found out.. i had it Debug every starting number, and i noticed it got stuck at one particular number, so then i made it debug all the items in the chain for that one number, and i noticed it was maxing the int.... |
Author: | cool dude [ Thu Jul 27, 2006 9:22 pm ] |
Post subject: | |
oh my god i can't believe this. after having my computer on for hours, this finished in under 5 seconds just by making my variable n. wow. thanks a lot short1. this is prolly a stupid question but how do you debug in JCreator. i know in visual basic you can put in breaks and go along each line, but how do you do it in java? if there are any tutorials on this please tell me. |
Author: | Cervantes [ Thu Jul 27, 2006 10:06 pm ] |
Post subject: | |
the_short1 wrote: Im curious if anyone has finished question 97
I tried with biginteger.pow.. and it wasnt getting anywhere.. ..and not gona finish in the next 2 days (i calculated it using mersenne.org's benchmarks).. hints? Just did it. It's really very easy. 6 lines of code, for me. Realize that you don't need to calculate the entire prime. That would just be ridiculous. You only need to know the last 10 digits. Now, look at the powers of 5. 5, 25, 125, 625... see something useful? |
Author: | wtd [ Thu Jul 27, 2006 10:30 pm ] |
Post subject: | |
May I say that I find it interesting that so many people are tackling potentially computationally intensive tasks in Java, rather than a more performance-oriented programming language. This is not to say "Java is slow(tm)" but rather that speed is not one of its primary goals, and there are other languages that do have this as a goal (and are not all pains in the butt like C). |
Author: | Cervantes [ Thu Jul 27, 2006 10:49 pm ] |
Post subject: | |
I'm doing them all in Ruby, which is even slower. ![]() It makes it more challenging. Doing these in O'Caml would be sweet, though. And it would be a good refresher. |
Author: | the_short1 [ Fri Jul 28, 2006 12:20 am ] |
Post subject: | |
wtd wrote: May I say that I find it interesting that so many people are tackling potentially computationally intensive tasks in Java, rather than a more performance-oriented programming language.
This is not to say "Java is slow(tm)" but rather that speed is not one of its primary goals, and there are other languages that do have this as a goal (and are not all pains in the butt like C). @ WTD.. i use java because java is a) FUN b) im fairly good at it ![]() @ Cool Dude... by Debug... i meant system.out.println... i grew tired of typing system.out.println every fricking time so i made a method called debug which prints whatever string you pass to it to screen and to text file... see java source code section..... @Cervantes...sometimes its the easy questions that kill ya.. must be having a slow day.. i see the connection, powers of 5 all end in 25...i did a question like that in discreet... but for twos .. i dont see anything usable.. i originally tried to think of a method like this but all i got was that its binary... |
Author: | wtd [ Fri Jul 28, 2006 12:37 am ] |
Post subject: | |
the_short1 wrote: @ WTD.. i use java because java is a) FUN b) im fairly good at it
![]() 1. Java may be fun for you. Lots of other languages are fun. Possibly more fun than Java. 2. Turing is not the only other choice. ![]() |
Author: | Cervantes [ Fri Jul 28, 2006 12:14 pm ] |
Post subject: | |
the_short1 wrote: ide like to see the person doing this in turing.. .ROFL.. wait .. if the person was able to do it in turing in <1minute for any of the harder questions.. the guy would be amazing :O Ruby is on approximately the same level of slowness as Turing. ![]() the_short1 wrote: @Cervantes...sometimes its the easy questions that kill ya.. must be having a slow day.. i see the connection, powers of 5 all end in 25...i did a question like that in discreet... but for twos .. i dont see anything usable.. i originally tried to think of a method like this but all i got was that its binary... What if I asked you the following question: "What are the last 2 digits in 5^513135?" Or what about, "What are the last 3 digits in 5^513135?" |
Author: | lapo3399 [ Fri Jul 28, 2006 7:50 pm ] | ||
Post subject: | |||
Does anybody use Matlab for these problems? I know that it isn't one of the more popular methods on these forums, but I find it very fast and concise. For example, the problem for finding the 10001st prime takes 3 lines in Matlab command line coding:
The second command takes about 15 seconds maximum on my computer. |
Author: | Null [ Sat Jul 29, 2006 9:28 am ] |
Post subject: | |
I did them initially in Ruby and Python and some C++ for the more resource intensive ones. I'm now going back and doing them all in C, which is quite fun. |
Author: | rizzix [ Sat Jul 29, 2006 10:08 am ] |
Post subject: | |
wtd wrote: speed is not one of its primary goals It sure is, maybe not explicitly.
The Java devs continually try to optimized the java compiler and the jvm. Don't believe me? Just look at the "bug fixes" yourself ![]() Besides a lot of the VM research going on has the jvm in focus and most of these research ideas are implemented in the jvm. |
Author: | lapo3399 [ Sat Jul 29, 2006 2:28 pm ] |
Post subject: | |
![]() |
Author: | Andy [ Sat Jul 29, 2006 2:31 pm ] |
Post subject: | |
i suggest you learn to read before replying. they were discusing Java VM, not the contest |
Author: | bugzpodder [ Sun Jul 30, 2006 12:26 pm ] |
Post subject: | |
Cervantes wrote: the_short1 wrote: @Cervantes...sometimes its the easy questions that kill ya.. must be having a slow day.. i see the connection, powers of 5 all end in 25...i did a question like that in discreet... but for twos .. i dont see anything usable.. i originally tried to think of a method like this but all i got was that its binary... What if I asked you the following question: "What are the last 2 digits in 5^513135?" Or what about, "What are the last 3 digits in 5^513135?" i dont get it... what are those two questions an example of? btw the answer to the first is 25. the answer to second is probably 125 or 625 but it shouldnt be that hard to figure out. |
Author: | Cervantes [ Sun Jul 30, 2006 11:28 pm ] |
Post subject: | |
The point is that we don't care about all the other digits. Furthermore, how would you determine the answer to the second one? You'd probably look for a pattern in the third last digit, then do the math. You could probably do that for this Euler question, too. But the other way to do it would be to use the computer to brute force it. I'm not saying to calculate all the digits of 5^513135. Just the last three. I don't want to completely give it away. |
Author: | bugzpodder [ Sun Jul 30, 2006 11:41 pm ] |
Post subject: | |
if you use maple, it does that for you. if you use any standard language, you'd use a for loop with a mod. |
Author: | Cervantes [ Sun Jul 30, 2006 11:43 pm ] |
Post subject: | |
Yeah, that's pretty much it. |
Author: | lapo3399 [ Mon Jul 31, 2006 5:31 pm ] |
Post subject: | |
Is it just me, or is the one they just added (124) relatively easy compared to the other higher-numbered problems? |
Author: | the_short1 [ Wed Aug 02, 2006 6:58 pm ] |
Post subject: | |
Now that my girlfriend is gone home i had a chance to do that question 97.. Thanks for the help... Executes <1s.. silly i didnt think of it myself, until ya said dont need the other digits then it hit me... then bugz confirmed... Good luck to the rest of youz still doing this contest. |
Author: | cool dude [ Wed Aug 09, 2006 8:16 pm ] |
Post subject: | |
i'm a little confused because i'm almost certain i have the correct answer but it tells me its wrong. the question was: Quote: Problem 37 14 February 2003 The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. the 11 truncatable primes i found were 23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 7331 when added together i get the answer 16251. however it tells me this is incorrect ![]() |
Author: | Andy [ Wed Aug 09, 2006 8:55 pm ] |
Post subject: | |
ummm i think you should look up the definition of the word prime |
Author: | [Gandalf] [ Wed Aug 09, 2006 9:11 pm ] |
Post subject: | |
7331 is not a left truncatable prime. ![]() |
Author: | cool dude [ Thu Aug 10, 2006 10:54 am ] |
Post subject: | |
Andy wrote: ummm i think you should look up the definition of the word prime
Andy i do know wat the definition of the word prime is, how else would i be able to solve the other prime problems eh? thanks gandalf small little mistake ![]() |
Author: | Andy [ Thu Aug 10, 2006 6:29 pm ] |
Post subject: | |
my point was that 1 is not a prime |
Author: | Martin [ Thu Aug 17, 2006 10:41 pm ] |
Post subject: | |
Alright Zylum, now it's on ![]() |
Author: | zylum [ Thu Aug 17, 2006 11:04 pm ] |
Post subject: | |
boo! ![]() i havent done this in like 2 months. |
Author: | zylum [ Thu Aug 17, 2006 11:35 pm ] |
Post subject: | |
your move ![]() |
Author: | Martin [ Fri Aug 18, 2006 12:17 am ] |
Post subject: | |
Fucker! That wasn't part of the plan. |
Author: | the_short1 [ Fri Aug 18, 2006 2:20 pm ] |
Post subject: | |
Martin wrote: ****er! That wasn't part of the plan.
hmm so your drealoth... . ... doh! why did you provoke the beast now he will kick both our asses ![]() ![]() |
Author: | cool dude [ Tue Aug 22, 2006 7:07 pm ] | ||
Post subject: | |||
i tried question 125 and i did it but its too slow. any suggestions on how to increase the speed?
P.S. forgive me on the variable names |
Author: | zylum [ Tue Aug 22, 2006 8:14 pm ] |
Post subject: | |
what i would do is generate all palindromes under 10^8 recursively. each time a palindrome is generated, check if it is the sum of squares. this can be done by using the formula s = n(n+1)(2n+1)/6. |
Author: | cool dude [ Tue Aug 22, 2006 8:41 pm ] |
Post subject: | |
zylum wrote: what i would do is generate all palindromes under 10^8 recursively. each time a palindrome is generated, check if it is the sum of squares. this can be done by using the formula s = n(n+1)(2n+1)/6.
thanks for the formula however wat if the consecutive numbers don't start from 1. for example 595 the consecutive numbers go 6,7,8,9,10,11,12 but the formula will go 1,2,3,4,5,...etc. trying to add up to 595 but that will never happen since it has to start from 6. so my question is how do i make the formula check all consecutive numbers for example. 3,4,5 or 6,7,8 so there starting point can be anywhere. |
Author: | zylum [ Tue Aug 22, 2006 9:46 pm ] |
Post subject: | |
yeah, thats why you look for two numbers such that s2 - s1 = the palindrome. |
Author: | the_short1 [ Tue Aug 22, 2006 11:17 pm ] |
Post subject: | |
Going through 100 mill in a for loop isnt too effective either... what i found worked really well, is to only create the first half of the pally, then mirror it.. and that eliminates the need to check if the number is pallindrome in the end as well. for example.. . for loop from 10 to 20. 10 01 11 11 12 21 13 31 etc.. and to create the odd digit ones, i would simply omit the first digit on the right half... eg 10 _1 (removed the 0) |
Author: | cool dude [ Wed Aug 23, 2006 10:38 am ] | ||
Post subject: | |||
would this be right?
even if this is right it still takes too long. sorry the_short1 but i don't understand your way. care to explain |
Author: | Cervantes [ Wed Aug 23, 2006 5:32 pm ] | ||
Post subject: | |||
There's a lot of ways you can optimize that. A *lot*. For starters, n does not have to range from 1..1000. There's no point having n > n2. That would give you a sequence of squares where the maximum is, say, 6^2 and the minimum is, say, 25^2. Your s2-s1 will be negative. Second, you should break out of those for loops if your conditional (s2 - s1 == i) is true. I spent some time today on this problem. Instead of ranging 'n' in a for loop and checking if it works, I developed an equation to predict what n should be, given a value for n2. It essentially amounts to solving the cubic:
where diff is the difference between the palindromic number and the sum of squares from 1 to n2. It worked beautifully, until the numbers started to get so large that my algorithm to solve the cubic (Cardano's method) produced Infinity as the solution. Perhaps I should switch to Newton's Method. |
Author: | cool dude [ Wed Aug 23, 2006 6:00 pm ] |
Post subject: | |
how would i break out of my for loop? also wat should i go up to in my for loop if not 1000? |
Author: | Cervantes [ Wed Aug 23, 2006 6:59 pm ] |
Post subject: | |
These are not difficult questions... The easiest way to break out of the loops is to factor them out into a function. You can return from the function at any time. The second for loop should go up to `n2 - 1' |
Author: | Cervantes [ Sun Aug 27, 2006 4:37 pm ] |
Post subject: | |
Ouch, zylum's taking a beating. ![]() |
Author: | the_short1 [ Mon Aug 28, 2006 6:40 pm ] |
Post subject: | |
i think what you mean cool dude .. . is this? Branching Statements In your case... its the "break" statement... Hope that helps, found it a few months ago... sadly our teacher in gr12 compsci didnt even teach us about "Break". . only "return" <for methods. While im on here tho, i have a question of my own... i completed question 18 a long time ago... and i tried using the same algorythm (actually i created a method to calculate the largest sum from triangle in text file 'xx') on question 67 (a triangle with 100 rows).. but its not cooperating.. Ide post the code, but its a dead giveaway for question18 then...as it works for that. Unless someone wants me to pm it.. It works by comparing the sum of the next 'precision' numbers, and precisions from 20-100 all produce the same (but wrong answer) of 7132 (i set it to output the results for precisions 1 to 100) How it works: eg: ---3 --7 5 -2 4 6 8 5 9 3 It tests the 7, the max sum is going 7 + 4 + 9, then tests the 5, max sum is 5 + 6 + 9 in this case its a tie (20), so it will choose the higher of the two (5,7) .. (so it picks 7).. then it chooses between 2 (2+8) or 4 (4 + 9).. and picks 4... so the maximum path sum in triangle is (3+7+4+9)...like i said it worked for q18.. .. but it doesnt for q67 :S Is this just faulty logic ? I just re read over my code agian today and it picks at me that it doesnt work... Btw.. it executes in 1s with precision 100. Lol... I know its kind of ironic how im asking for help even tho im at 900+ points.. but im not "too proud" to ask for help.. And Thanks for any suggestions. -kevin |
Author: | Martin [ Mon Aug 28, 2006 9:39 pm ] |
Post subject: | |
500 bits to the first person from this site to get into the top 200, and post a screenshot of it. |
Author: | Cervantes [ Wed Aug 30, 2006 9:10 pm ] |
Post subject: | |
the_short1: Didn't you read the note at the bottom of Q67? It completely rules out your approach. If you read zylum's recursion tutorial, you should be able to write a program that will solve that question in a second or less. With ease. |
Author: | zylum [ Thu Aug 31, 2006 12:15 am ] |
Post subject: | |
did a few today. should catch up to the_short1 soon. |
Author: | the_short1 [ Thu Aug 31, 2006 5:06 pm ] |
Post subject: | |
zylum wrote: did a few today. should catch up to the_short1 soon.
Not soon enuf.. ![]() |
Author: | neufelni [ Thu Aug 31, 2006 7:41 pm ] | ||
Post subject: | |||
I just started Project Euler today and am working on question 5. What is the smallest number divisible by each of the numbers 1 to 20? I made this program with Turing to solve it, but its too slow.
It works properly, I tested it with checking the numbers 1-10 instead of 1-20 and it gave me the right answer in a few minutes. But now I've been running the program for several hours and it has not yet given me the answer. How can I make it faster? |
Author: | cool dude [ Thu Aug 31, 2006 9:00 pm ] |
Post subject: | |
well having 2 loops like that will take up a lot of time. why don't u try having one loop and one for loop. basically make a loop and make a variable that will hold the actual number where digits 1 - 20 are divisible by it. so now everytime u go through the loop make that number increase by 1260. u may ask why 1260 well its because if u multiply 20 x 9 x 7 you will get 1260. then u may ask why u don't multiply by 1,2,3,4...20 the answer is that every 1260 if 20, 9, 7 are divisible by it all the other numbers will be divisible as well. so now that u increase the number by 1260 everytime through the loop you want to check if those numbers are actually divisible by 1 to 20 so make a for loop and check if the number is evenly divisible by the digits 1 to 20. if not make a boolean variable and setting it to false. but in the start of the loop initialize the boolean variable to be true. so that once it goes through the for loop and checks to see if 1 to 20 are not divisible by the number it will set it to false. now u exit the loop once the boolean will be true. follow this step by step and that should give u a quick answer. btw it ran in less than 5 s. this might be a bit hard to understand so ask questions if needed. srry i can't post code. if your really stuck there's a really easy method to do it with paper and pencil that u can check up on the internet. good luck |
Author: | Ultrahex [ Thu Aug 31, 2006 11:56 pm ] |
Post subject: | |
Also The Other Thing you forgot is simple things ... like ... num mod 10 and num mod 20... they are the same... if it mods by 20 then it will mod by 10 because 20 mod 10 is 0 10=20 9=18 8=16 7=14 6=12 5=10=20 4=8=16 3=6=12 2=4=8=16 1=Always Works on All Natural Numbers so if you use some simple reasoning (which i basically already explained) you should be able to come up with the only numbers you need to mod with which is a lot less! (11,12,13,14,16,17,18,20) (or 11 to 20 but dont have to bother with 15) (note going up by 1 is pointless) Using this method it took me.... 20 ? seconds (19353ms) on my 1.4GHZ There is also faster ways ![]() |
Author: | neufelni [ Fri Sep 01, 2006 8:42 am ] | ||
Post subject: | |||
Alright, I got it right. I decided to use Java instead and I made this program.
It is still pretty slow, it took about a minute. |
Author: | richcash [ Fri Sep 01, 2006 9:54 am ] |
Post subject: | |
Nick, you should use java for these problems from now on, as they become almost (and maybe even) impossible in Turing. I only have Turing, so what can I do, lol. I started 3 days ago and gave up on the 8th most difficult. This problem in particular was very simple! You could have even just used a calculator in your hand. These are the steps : 1. You have the GCF for 1 to 10, so check if it's divisible by the next number (11) 2. If it's not divisible (11 isn't), then multiply (your current GCF) by the lowest factor of the number (11) and see if the number (11) divides into that (11 has no factors). If not, go back to the original GCF and try the next lowest factor. 3. If none of the number's factors work, or the number is prime, multiply by the number and move on to the next number (you have to do this for 11). |
Author: | cool dude [ Fri Sep 01, 2006 10:36 am ] | ||
Post subject: | |||
k bak to problem 125. its still taking too long to run and its driving me crazy. i am breaking out of the for loops when my conditional (s2 - s1 == i) is true. is there any more ways to optimize it? here's my code:
|
Author: | wtd [ Fri Sep 01, 2006 10:59 am ] | ||||
Post subject: | |||||
Nick wrote: Alright, I got it right. I decided to use Java instead and I made this program.
It is still pretty slow, it took about a minute. A reasonably direct translation to SML/NJ. This computed the result almost instantly.
|
Author: | richcash [ Fri Sep 01, 2006 11:07 am ] | ||
Post subject: | |||
And while you're helping cool_dude (sorry, no help, don't know java, but to me it looks like your code shouldn't be more than a minute?), can you tell me what I'm doing wrong on problem 10 to calculate all the primes under 1 million? Here's my code in Turing :
So, I tried 370 015 402 174 (with no spaces). I also tried 37 015 402 174, 37 015 402 175, and 370 015 402 175. What's going on here??? |
Author: | neufelni [ Fri Sep 01, 2006 1:13 pm ] |
Post subject: | |
I'm working on problem 17 now and I am stuck. First I used pencil and paper and made a list of all the basic words used like one, two, twenty, hundred .... I figured out how many times they appeared in the range of 1-1000 and multiplied it by the number of letters in each word, and added them all together, but that didn't give me the right answer. So I typed every number between 1-1000 with a word processor and did a character count, but that didn't give me the right answer neither. I have no idea how I would make a program to solve this, so please help me. Here's the list of numbers I made with Word. I checked through it to see if I made any spelling mistakes or anything like that but I didn't find anything. |
Author: | cool dude [ Fri Sep 01, 2006 1:52 pm ] |
Post subject: | |
wow u actually sat there typing out all the numbers when it would take u so little time to make a program lol. i don't feel like checking all the numbers to check for spelling mistakes. to make a program u would use arrays which would be really really easy. P.S. u are close with the character count. u must have missed one number. i don't think u made a spelling mistake. check if u didn't miss a number ![]() |
Author: | neufelni [ Fri Sep 01, 2006 2:08 pm ] |
Post subject: | |
cool dude wrote: wow u actually sat there typing out all the numbers when it would take u so little time to make a program lol. i don't feel like checking all the numbers to check for spelling mistakes. to make a program u would use arrays which would be really really easy.
P.S. u are close with the character count. u must have missed one number. i don't think u made a spelling mistake. check if u didn't miss a number ![]() I didn't type it all out, I did alot of copying and pasting. And I did not miss a number, the word count says I have exactly 1000 words. EDIT: I just found one spelling mistake but it's still not right, there must be more. |
Author: | the_short1 [ Fri Sep 01, 2006 2:08 pm ] | ||
Post subject: | |||
[Edit: that was silly.. . i need to sleep more.. my job is killing my ability to think.. lol] Although, my method for checking pally's checks individual characters...because i wanted to let it work for words like "lol" roughly whipped up
or do my previous suggestion, that avoids testing for pallindromes sinec every itteration of the for loop would be a pally no matter what. |
Author: | cool dude [ Fri Sep 01, 2006 2:18 pm ] |
Post subject: | |
hmm... did u try putting a system.out.println. it passes all the palindromes. if i'm not mistaken a palindrom is when a number is reversed and is still the same. for example. 77 when reversed will be 77 and since both are equal its a pally. 123456789 is not a pally therefore it should never pass and it does not. i think your a bit confused. try looking at it again. the slow part isn't even in my searching for a palindrom but its when i'm checking if its the sum of consecutive square numbers. @Nick: your very close! keep searching. hint: your a bit under so your character count should be more |
Author: | neufelni [ Fri Sep 01, 2006 2:31 pm ] |
Post subject: | |
I just got the right answer for 17. I forget the first e in nineteen. |
Author: | the_short1 [ Fri Sep 01, 2006 2:33 pm ] |
Post subject: | |
i know 123456789 isnt a pally... something like 51215 is..... . indeed i was a bit confused.. i see my mistake and cleared that part of my post as its irrelevant now.. Sorry ![]() |
Author: | cool dude [ Fri Sep 01, 2006 2:52 pm ] |
Post subject: | |
the_short1 wrote: i know 123456789 isnt a pally... something like 51215 is..... . indeed i was a bit confused.. i see my mistake and cleared that part of my post as its irrelevant now.. Sorry
![]() thanks short_1 but how would u suggest searching if the pally is also the sum of consecutive square numbers? i used the formula s1 = (n * (n + 1) * (2 * n + 1)) / 6; and since it gets the sum of all the consecutive square numbers starting from 1 i had to do it again and subtract s2 - s1. so in theory its correct but its too slow. |
Author: | the_short1 [ Fri Sep 01, 2006 4:13 pm ] | ||
Post subject: | |||
Well mine didnt use a formula and i just executed in...100s.. but im listening to music and i have a BitTorrent going.. so im sure if i was on family pc it would be well under 60seconds... well basically.. mine just has a for loop for the starting base.. then an interior for loop for the continuing bases breaking when the sum exeeds the pally... and doesnt leave the outer for loop until that number squared is bigger then the pally.. nothing fancy and it should work fine. now that i know that formula maybe ill redo it ![]() to generate pally's every time.. without having to verify..and many less itterations = faster..
And nick... if you know java.. i would suggest using Java's BigInteger and its "num.isProbablePrime(5);" function.. where num is a BigInteger that you want to test.... . it works adequately when primes start getting bigger.. .but one of the most efficient is the "Sieve of Eratosthenes".. google it |
Author: | Cervantes [ Fri Sep 01, 2006 4:27 pm ] | ||
Post subject: | |||
Let's say n1 and n2 are 0 or natural numbers. We're interested in the sum of the squares of all whole numbers between n1+1 and n2. So if n1 is 5 and n2 is 8, we're interested in 6^2 + 7^2 + 8^2. That value can be calculated by summing the squares of the natural numbers from 1 to n2 then subtracting the sum of the squares of the natural numbers from 1 to n1. Like this:
Now, we're interested in determining whether a number, such as 595, can be written as the sum of the squares of consecutive natural numbers. We might pick a value for n2 and pick another value for n1 and use the formula to see if we get 595 or not. If we generate all possible combinations of n2 and n1 such that n1 < n2 and n2 < floor(sqrt(595 / 2)), we can perform a check. But it's slow. Too many iterations. We can remove an entire loop. Replace it with some math. Once we've picked a value for n2, we don't need to iterate over values of n1. Using math, we can calculate what n1 must be in order to get our sum to equal 595. Quite often the solution will state that n1 must be something like 5.7 or some other real number. That would be telling you that 5.7^2 + 6.7^2 ... = 595. But we're only interested in natural numbers, so we disregard that, increase n2, then check n1 again. I'll tell you right now that this method requires you to be able to solve a cubic equation. Even if you can do that, it's a bit slow. In a fast language the program would probably run in time, but in Ruby, mine didn't. I found another way quite similar to this that only required me to solve a quadratic. This is much faster. |
Author: | neufelni [ Fri Sep 01, 2006 4:27 pm ] |
Post subject: | |
the_short1 wrote: And nick... if you know java.. i would suggest using Java's BigInteger and its "num.isProbablePrime(5);" function.. where num is a BigInteger that you want to test.... . it works fastest when primes start getting bigger.. .but one of the most efficient is the "Sieve of Eratosthenes" I don't know Java all that well, I just know the basics. I probably know enough to solve most of these problems, but not very fast and efficiently. I am best with Turing and also know a bit of C++ and Python. I tried using BigInteger for Problem 16, What is the sum of the digits of 2**1000, but I couldn't figure out how to use it. So I used Python and it gave me the answer without having to using anything special. |
Author: | the_short1 [ Fri Sep 01, 2006 5:32 pm ] | ||
Post subject: | |||
@Cervantes.. are you in uni now? i suck at deriving formulas from scratch.. .. lol.. . the equation narrows down to this if my math serves me today..? (2*n2^3 + 3*n2^2 + n2) - (2*n1^3 + 3*n1^2 + n1) = num * 6 @Nick: Python.. thats something im curious to learn ![]()
the string digits now contains the massive result, whose digits can be counted simply with a for loop and substrings, and Integer.parseInt |
Author: | wtd [ Fri Sep 01, 2006 5:42 pm ] | ||
Post subject: | |||
the_short1, just for demo purposes, that piece of Java code can be rwritten in Python as:
Perhaps that's a bit more incentive to learn a new language. ![]() |
Author: | Cervantes [ Fri Sep 01, 2006 7:17 pm ] |
Post subject: | |
the_short1 wrote: @Cervantes.. are you in uni now?
I'm in limbo. I move in Sunday. ![]() the_short1 wrote: i suck at deriving formulas from scratch.. .. lol.. . the equation narrows down to this if my math serves me today..?
(2*n2^3 + 3*n2^2 + n2) - (2*n1^3 + 3*n1^2 + n1) = num * 6 That looks familiar. Now, set that up in the form of a quadratic for n1 (a*n1^3 + b*n1^2 + c*n1 + d = 0). n2 is constant (we iterate through values of n2, but at this time we treat it as a constant), so the n2 values and the num*6 will together comprise `d'. Now to solve, you've got a few options. The one I like is Cardano's method. It's fast; almost as fast as solving a quadratic. But the problem with it is that when the root gets extreme, it gives trouble to a computer. As I recall, you had a number divided by a really tiny (close to zero) number, then multipled by another really small number, giving you a decent sized number. But as soon as you do the first division, the number is too big to fit in memory. Or something like that. So your other alternative is to use [url=http://en.wikipedia.org/wiki/Newton's_method]Newton's Method[/url]. This will work, but it is slow because it is iterative. It's only an approximation of the roots. [/quote] |
Author: | the_short1 [ Fri Sep 01, 2006 9:03 pm ] |
Post subject: | |
indeed... i read a bit of other python code on project euler forums and i liked what i read but for robotics .. . it would be better to focus on learning better C skills / c++.... but .. ide just like to mention.. i got around to implementing a sieve... and im totally impressed... . first 10 million primes into an array.. in 3seconds.. amazing and ide avise any other prime seekers to follow same route.. @ Cervantes.. hmm... well both methods dont sound too great. As page file memory sucks the speed out of things.. . and aproximations and slowness doesnt sound good either.... hmm well im off to work... ill try the cubic tomorow using the first method. thanks |
Author: | Cervantes [ Fri Sep 01, 2006 9:30 pm ] |
Post subject: | |
Don't worry too much about approximations. If you get an answer of 5.00027193 then you can bet that you've got an integer solution. If you like, you could then check the equation using 5.0. What's this about "page file memory"? |
Author: | cool dude [ Sat Sep 02, 2006 12:22 pm ] |
Post subject: | |
how would i determine if the number is a decimal or not? |
Author: | Clayton [ Sat Sep 02, 2006 1:19 pm ] | ||
Post subject: | |||
round the number down and divide the non-rounded number by the rounded one and see if theres a remainder?
something like that? |
Author: | neufelni [ Sat Sep 02, 2006 1:51 pm ] |
Post subject: | |
Can anyone give me some tips for making programs run faster. All of the programs I make run really slow. |
Author: | zylum [ Sat Sep 02, 2006 2:47 pm ] |
Post subject: | |
choose a better algorithm. some require the answer to meet multiple criteria. to meet these criteria you probably use nested loops. the outter loops should be for the coarsest criteria, therefore there are fewer inner loops. also look for ways to reduce the amount of loops you need to do and criteria you have to meet. some criteria might overlap. |
Author: | neufelni [ Sat Sep 02, 2006 3:17 pm ] | ||
Post subject: | |||
Here is my code in Python for Problem 3, what is the largest prime factor of 317584931803.
First I check if each number is divisible by 317584931803, then if it is I check to see if it is a prime number. I used Python because it handles big numbers better than Java or C++, I don't have to use something like BigInteger. I think this is the best way to have as few inner loops as possible. I already break out of the inner loop the moment I find out that it isn't a prime number, but it's still way too slow. How could I make this faster? |
Author: | wtd [ Sat Sep 02, 2006 3:36 pm ] | ||||
Post subject: | |||||
Since the first thing to run in your loop is a conditional break, how about...
|
Author: | Cervantes [ Sat Sep 02, 2006 4:15 pm ] |
Post subject: | |
There's no sense in starting that loop with num equal to that huge number. Instead of that huge number, let's say we're working the number 14. There's no sense in checking whether 13, 12, 11, 10, 9, or 8 are divisible by 14. The biggest number that could possibly be a factor of 14 is 14/2 = 7. So it doesn't make much sense starting from the top and working down since the numbers are much less common. Rather, start from 2, and work up. 14 / 2 = 7, so check if 7 is prime. It is, but if it weren't, then you'd go to 14/3 = 4.67 and check if that is an integer and if it's prime, and so on. Also, factor out that code that checks whether a number is prime. It's very useful to have in these problems, and it's just good coding practice to make a method out of something as fundamental as 'prime?' |
Author: | neufelni [ Sat Sep 02, 2006 6:27 pm ] |
Post subject: | |
OK, I got the answer. I tried the method you suggested. It started out really fast but got progressively slower until it took about 2 seconds for num to decrease by 1. So I added an if statement so that it used your method while num > 200 000, and then it switched to my old method of decreasing num by one each time. |
Author: | the_short1 [ Sat Sep 02, 2006 9:55 pm ] |
Post subject: | |
@ Cervantes: you said the number got too big to fit in memory, hence after that it would use page file memory instead ![]() @ Nick: Zylum hit the head on the nail... But ill add my $0.02.. less itterations, less comparisons, less complex math (avoid sqrt)... Another thing i try to do, is output the values known to be wrong to find hidden connections.... like, in one question it is quite ovious that prime numbers wont be the right ones, so i skip past them |
Author: | Cervantes [ Sat Sep 02, 2006 10:06 pm ] |
Post subject: | |
the_short1 wrote: @ Cervantes: you said the number got too big to fit in memory, hence after that it would use page file memory instead
![]() I'm pretty sure the number never actually got so big it couldn't be stored in all 512 of my megabytes of memory. But it did get too big for Ruby to deal with it, so it just labelled it as "NaN" for "Not a Number". |
Author: | Andy [ Tue Sep 05, 2006 3:25 pm ] |
Post subject: | |
the_short1 wrote: @ Cervantes: you said the number got too big to fit in memory, hence after that it would use page file memory instead
![]() based on that comment, i dont think you know much about the memory architecture... TLB makes physical and virtual memory into a big contiguous chunk |
Author: | Cervantes [ Tue Sep 05, 2006 5:00 pm ] |
Post subject: | |
No, I don't. But I'm telling you, the number I was dealing with wouldn't have been greater than or equal to 2^((512 - memory used by OS) * 1024 * 1024 * 8). |
Author: | Andy [ Tue Sep 05, 2006 5:18 pm ] |
Post subject: | |
oh minsc... the comment was directed towards the short |
Author: | cool dude [ Tue Sep 05, 2006 5:35 pm ] | ||
Post subject: | |||
k cerventes thanks for the suggestion. i tried it out and i don't know where i went wrong but it's not working and i get no error messages. basically i did wat u told me by subtracting s2 from i to figure out wat s1. then i had to check if s1 value is actually a value that is the sum of consecutive squares so in order to do so i had to solve a cubic equation as u told me. the equation is: 2x^3 + 3x^2 + x - s2 = 0 at first i didn't know how to solve it so i went to this site: http://www.1728.com/cubic2.htm which will prolly help u understand my program a lot better. so i'm guessing i'm solving the cubic formula correctly and it should work but it doesn't. any help? here's my code:
p.s. i know its really messy code but if you look at the site i gave above how to solve a cubic you will understand all those variable names i.e. h, g, u, m, n, x1, x2, x3, etc... help would be appreciated ![]() |
Author: | Cervantes [ Tue Sep 05, 2006 7:40 pm ] |
Post subject: | |
Andy wrote: oh minsc... the comment was directed towards the short
Ah, blast. The "@cervantes" got me. ![]() cool dude: If I understand you're description correctly, you've misinterpreted what (I think) I said. I said nothing about a 'i' variable that you subtract s2 from. Rather, you have your value for 'n' (your palindrome) and 's2' (the number that, when squared, is the high end of the list of consec. squares). You solve a cubic to determine the value for s1, then check if it's a whole number. edit: Andy wrote: ... minsc ...
Bwah? You don't even go on IRC! ![]() |
Author: | Andy [ Tue Sep 05, 2006 8:27 pm ] |
Post subject: | |
sure i do.. once in a while |
Author: | cool dude [ Tue Sep 05, 2006 9:04 pm ] |
Post subject: | |
srry cerventes i don't think i understand u. mind providing an example please? |
Author: | neufelni [ Wed Sep 06, 2006 7:49 pm ] |
Post subject: | |
I have managed to get quite a few of these answers now. They aren't all that difficult once you get quite a few of them done. I have also gotten past cool_dude in the scores. |
Author: | cool dude [ Wed Sep 06, 2006 10:07 pm ] |
Post subject: | |
Nick wrote: I have managed to get quite a few of these answers now. They aren't all that difficult once you get quite a few of them done.
I have also gotten past cool_dude in the scores. ya unfortunately school started for me so i will put this off for a while. i'm still gonna try to get this problem 125 that took most of my time. they aren't that hard if u know the language your writing in. as some of you know i.e. wtd i just started learning java and all these questions i was doing in java not knowing any of the syntax. fortunately now i know much more java ![]() Edit: winning again ![]() |
Author: | the_short1 [ Thu Sep 07, 2006 5:00 pm ] |
Post subject: | |
Andy wrote: the_short1 wrote: @ Cervantes: you said the number got too big to fit in memory, hence after that it would use page file memory instead
![]() based on that comment, i dont think you know much about the memory architecture... TLB makes physical and virtual memory into a big contiguous chunk ![]() *hence after that it would also use page file memory* Yea with work and school.. I layed off PE as well... I Challenge someone to take over the compsci spot for number one(rank higher then me), the reward will be 200 bits to the first person to post a screenshot(of rankings) on this thread & pm me... Seeing after i won the 500 bits from martin (too easily, as i only had to answer 2 more questions)... thought i would give some back.. .since i have over 5k anyways ![]() |
Author: | cool dude [ Thu Sep 07, 2006 5:57 pm ] |
Post subject: | |
for problem 28 is there a pattern? |
Author: | neufelni [ Thu Sep 07, 2006 6:42 pm ] |
Post subject: | |
cool dude wrote: for problem 28 is there a pattern?
Yes there is a pattern and the question is quite easy when you find out what the pattern is. |
Author: | cool dude [ Thu Sep 07, 2006 10:21 pm ] |
Post subject: | |
Nick wrote: cool dude wrote: for problem 28 is there a pattern?
Yes there is a pattern and the question is quite easy when you find out what the pattern is. any hints for the pattern? i'll look at it later when i'm not soo tired |
Author: | zylum [ Thu Sep 07, 2006 11:42 pm ] |
Post subject: | |
dont look for patterns. just code it. shouldnt take more than a few minutes and or lines of code. |
Author: | neufelni [ Fri Sep 08, 2006 9:31 pm ] |
Post subject: | |
cool dude wrote: Nick wrote: cool dude wrote: for problem 28 is there a pattern?
Yes there is a pattern and the question is quite easy when you find out what the pattern is. any hints for the pattern? i'll look at it later when i'm not soo tired I think there is more than one pattern but the one I used was a pattern in the diagonals going out from the center in each direction. I made the spiral a bit bigger than 5x5 on paper and then I found the pattern and then made a program to calculate it for the bigger spiral. |
Author: | neufelni [ Fri Sep 22, 2006 9:06 pm ] | ||
Post subject: | |||
I have been working on Problem 23 for the past few days. I wrote this program in Python but its giving me the wrong answer.
I am not sure what I am doing wrong. It seems to give me the right list of abundant integers, since the first one it gives me is 12, which the Project Euler site said was the lowest. So the problem must be in the second part, but I can't figure it out. Can anyone help? |
Author: | wtd [ Fri Sep 22, 2006 11:00 pm ] | ||||||||||
Post subject: | |||||||||||
Nick wrote: I have been working on Problem 23 for the past few days. I wrote this program in Python but its giving me the wrong answer.
Can be expressed as a generator passed to the built-in sum function.
So now we have:
But that first for loop can be further simplified.
A little sample of idiomatic Python. Not sure if it helps, but felt like offering some feedback, especially when a moderately interesting language is being used. ![]() |
Author: | Justin_ [ Sat Oct 07, 2006 2:18 pm ] | ||
Post subject: | |||
For Problem 10. Find the sum of all the primes below 1 million. I've got this code which I don't see the problem with.
3190663655 |
Author: | Justin_ [ Sat Oct 07, 2006 2:34 pm ] |
Post subject: | |
Oh... Well I learned that there is a such thing as a long long int today. Cool. I just made total a long long int and it found the correct answer. |
Author: | neufelni [ Tue Oct 10, 2006 8:54 pm ] | ||
Post subject: | |||
I am working on Problem 37 now, the one where you have to find the sum of all primes that are truncatable both from left to right and rigth to left. There are only supposed to be eleven of them but this program that I made, which only goes up to ten thousand, gives me quite a bit more than eleven. Why is this? Here is my code in Python:
And here are the results that it gives me: Quote: ---------- Capture Output ----------
[/code]> "C:\Python24\python.exe" Problem_37.py 11 13 17 23 31 37 53 71 73 113 131 137 173 197 311 313 317 373 797 1373 1997 3137 3797 7331 Sum: 20826 > Terminated with exit code 0. |
Author: | Andy [ Wed Oct 11, 2006 4:33 pm ] |
Post subject: | |
1 isnt a prime |
Author: | cool dude [ Fri Oct 13, 2006 7:14 pm ] |
Post subject: | |
Andy wrote: 1 isnt a prime
ha thats exactly the mistake i made and thats exactly wat you told me and you are correct |