Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 500 501 [502] 503 504 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 908754 times)

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7515 on: June 05, 2015, 07:58:15 pm »

Did Project Euler problem 1. At first I thought I'd make a vector containing all the numbers from 1 to the goal number variable, then run through the vector and if the slot was either slot%3==0 or slot%5==0 then I'd add that slot to a variable storing the end sum. Then I changed that to an array. Then I scrapped the array and just made a for loop.

Seems to be right. It gets 23 when made to calculate from 1 to 10.
Spoiler (click to show/hide)

I probably could make it faster / more efficient by taking everything in the class and just putting it in main, but eh. Would rather feel like I'm doing some kind of object oriented programming. Or at least attempting to do object oriented programming. :v

Project Euler seems fun. I always liked the little programming assignments I'd have to solve in comp sci classes.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #7516 on: June 05, 2015, 08:02:26 pm »

Spoiler: Problem 1 in Python (click to show/hide)
« Last Edit: June 05, 2015, 08:15:31 pm by Orange Wizard »
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7517 on: June 05, 2015, 08:12:27 pm »

Python has the % modulo operator, y'know.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #7518 on: June 05, 2015, 08:15:36 pm »

Shush.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7519 on: June 05, 2015, 09:55:00 pm »

Solved Problem 3 on Euler. These are more fun than I think they should be.

Spoiler (click to show/hide)

Gets 6857 out of the goal of 600,851,475,143.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7520 on: June 05, 2015, 10:57:43 pm »

Quote
I probably could make it faster / more efficient by taking everything in the class and just putting it in main, but eh. Would rather feel like I'm doing some kind of object oriented programming. Or at least attempting to do object oriented programming. :v

EDIT: OOh yeah I got the Euler#1 solution down to a single line operation you could do on a pocket calculator!
Spoiler (click to show/hide)

« Last Edit: June 05, 2015, 11:17:31 pm by Reelya »
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7521 on: June 06, 2015, 03:39:38 am »

Formula for the sum of a row un is sn = n * (u1 +un) / 2, so you just find the highest multiple, calculate it's position in the row, do this for the other number and add them.

FAKEDIT: just realized that you probably can't add a number twice, which makes formulas significantly harder. E2: oh Reelya figured it out, but at least I provided where the formula comes from.

Also @Spessh: check out UVA Online Judge, it has a fuckton of programming problems.

E3: Also Spessh your prime factorization is horribly inefficient ;~; (but I forgot how you're supposed to make a more efficient version)
« Last Edit: June 06, 2015, 03:42:57 am by miauw62 »
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7522 on: June 06, 2015, 03:53:18 am »

For the primes you should make something that spits out all primes <= num/2, then check against that list. No need to check all numbers up to num (but you should check whether num itself is prime).

There are definitely quite a few tricks you can do to generate a list of primes. Even just naive changes can speed up the process quite a bit. First up, other than 2, all primes will be odd. That halves your search space right away. Then you can realize the same things holds for 3, so you can now step up 6 places per loop, and only check the things that are not multiples of 2 or 3. so you check in this pattern:

// pre-check against 2 and 3 and 5 factors here.

for(int i = 6; i<=num/2;i+=6)
{
    // first loop of i checks against 0,1,2,3,4,5. second loop if i checks aganist 6,7,8,9,10,11 etc
    // i+0, i+2, i+4 are invalid (even), as is i+3 (multiple of three)
    // so for each loop, you need to check i+1 amd i+5 only
   
    // so this halves the checks (num/2) then checks only 1/3 remaining values. Total speedup factor = 6
}

You could extend this to also including factors of 5 in the pattern, but then you're skipping 30 places at once so it would be a bit more messy, and there are diminishing returns since you're only ruling out additional factors of i+5, i+25 out of every 30. The other multiples of 5 were already covered by being divisible by 2 or 3. Hence, this particular speed up is only viable up to factors of 3.
« Last Edit: June 06, 2015, 04:07:00 am by Reelya »
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7523 on: June 06, 2015, 04:06:34 am »

For the primes you should make something that spits out all primes <= num/2, then check against that list. No need to check all numbers up to num (but you should check whether num itself is prime).

You can check up to sqrt(num) instead of num/2 (since you can take num/(number divisible by num) to get the number on the opposite side of the sqrt).

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7524 on: June 06, 2015, 06:24:19 am »

Actually you could exploit that further by taking into account that you only need to calculate up to the sqrt of the remaining unfactored part, not the sqrt of the original number. e.g. (pseudocode)
Spoiler (click to show/hide)
« Last Edit: June 06, 2015, 07:31:13 am by Reelya »
Logged

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7525 on: June 06, 2015, 07:01:34 am »

E3: Also Spessh your prime factorization is horribly inefficient ;~; (but I forgot how you're supposed to make a more efficient version)
It works though. :v
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7526 on: June 06, 2015, 07:30:46 am »

Well I checked that the snippet of code I provided in the last post works by solving Euler problem #3 with it.

TheDarkStar

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7527 on: June 06, 2015, 09:45:03 am »

I've just started the Euler problem archive. Now I have to relearn file I/O because I don't want to enter the gigantic number for Problem 8.  :-\
« Last Edit: June 06, 2015, 09:47:23 am by TheDarkStar »
Logged
Don't die; it's bad for your health!

it happened it happened it happen im so hyped to actually get attacked now

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #7528 on: June 06, 2015, 03:30:04 pm »

Do you guys know a way to solve a connected components problem that isn't brute force? The brute force method looks like this. Basically, you loop over every single tile, skipping ones that have already been flood-filled and doing a depth-first search otherwise. The grid shown in that example is pretty small, but I'm working with a pseudo-rectangular hex grid of size 128x128. It creates a noticeable frame drop whenever I run it. Not too bad, but I want to improve it if possible.

The tiles are terrain types, all but one of them being land. The flood fill detects contiguous chunks of terrain tiles of the same type, but in addition to this I need to detect water and landmasses separately. A landmass can have many terrain type regions in it. Right now, I'm thinking of saving the coordinate of a single tile in each region, then after I detect all the regions I'll perform an A* pathfinding check between regions to tell if they're in the same landmass. Summing up the sizes of the regions gives me the landmass size.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7529 on: June 06, 2015, 03:51:55 pm »

I've just started the Euler problem archive. Now I have to relearn file I/O because I don't want to enter the gigantic number for Problem 8.  :-\
Can't you just copy-paste the string into your code instead of into a separate file?

Do you guys know a way to solve a connected components problem that isn't brute force? The brute force method looks like this. Basically, you loop over every single tile, skipping ones that have already been flood-filled and doing a depth-first search otherwise. The grid shown in that example is pretty small, but I'm working with a pseudo-rectangular hex grid of size 128x128. It creates a noticeable frame drop whenever I run it. Not too bad, but I want to improve it if possible.

The tiles are terrain types, all but one of them being land. The flood fill detects contiguous chunks of terrain tiles of the same type, but in addition to this I need to detect water and landmasses separately. A landmass can have many terrain type regions in it. Right now, I'm thinking of saving the coordinate of a single tile in each region, then after I detect all the regions I'll perform an A* pathfinding check between regions to tell if they're in the same landmass. Summing up the sizes of the regions gives me the landmass size.
Technically, the floodfill algorithm is the fastest possible to detect contiguous tiles. If you're getting frame drops, you're definitely doing something wrong, because floodfill runs in linear time, which should be done in an instant considering how small your grid is.

Could you maybe describe your problem in more detail? Why, when and how often are you doing a floodfill? I need more context please. Also maybe some source. After all, you could be doing something entirely different wrong.
Logged
Pages: 1 ... 500 501 [502] 503 504 ... 796