Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 196 197 [198] 199 200 ... 796

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

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2955 on: September 27, 2012, 06:20:54 am »

Your version doesn't find largest prime factors larger than sqrt(n), since the algorithm stops before there. If you'd let it continue, it would take ages.
Logged

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2956 on: September 27, 2012, 06:34:24 am »

You can make it go even faster by doing: (I would think)
Code: [Select]
@timed
def lpf2(n):  # had to modify a bit to accommodate Python
p = 2
if (p * p <= n):
while (n % p == 0):
n /= p
p += 1
while (p * p <= n):
if n % p == 0: n /= p
else: p += 2
return n

i.e. from 3 onwards stepping 2 at a time as prime numbers above 2 are always odd. If you unrolled it a bit more then after 3 you could step 2,2,4,2,4,2,4,2,4 to skip any multiples of 3, which would save a lot of redundant tests.
« Last Edit: September 27, 2012, 06:41:35 am by Thief^ »
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2957 on: September 27, 2012, 06:36:32 am »

Your version doesn't find largest prime factors larger than sqrt(n), since the algorithm stops before there. If you'd let it continue, it would take ages.
The way the 2nd algorithm works it has already removed all prime factors below sqrt(n) from n, so it's impossible for there to be any prime factors left above n except n itself.
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2958 on: September 27, 2012, 06:57:34 am »

Your version doesn't find largest prime factors larger than sqrt(n), since the algorithm stops before there. If you'd let it continue, it would take ages.
The way the 2nd algorithm works it has already removed all prime factors below sqrt(n) from n, so it's impossible for there to be any prime factors left above n except n itself.
What happens if n is 202?
Logged

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2959 on: September 27, 2012, 07:59:05 am »

It divides it by 2, then is left with 101. It then keeps going until it gets to 11, then calls 101 the highest prime factor.
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2960 on: September 27, 2012, 09:01:45 am »

I've written various algorithms over the past year or so for testing primality. I'll do some speed tests today after class, just because.

Also, anyone have any ideas about my Java problem?

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2961 on: September 27, 2012, 09:44:39 am »

Question: Is there a good reason to do this:
Code: [Select]
class Point:

    def __init__(self, initX, initY):
        self.x = initX
        self.y = initY

    def getX(self):
        return self.x

    def getY(self):
        return self.y

    def line_function(self,other):
        x_diff = other.x - self.x
        y_diff = other.y - self.y
        a = y_diff / x_diff
        b = self.y - a * self.x
        return (a,b,)

p = Point(0,17)
q = Point(5,27)

print(p.line_function(q))

instead of this:

Code: [Select]
class Point:

    def __init__(self, initX, initY):
        self.x = initX
        self.y = initY

    def getX(self):
        return self.x

    def getY(self):
        return self.y

def line_function(first,other):
        x_diff = other.x - first.x
        y_diff = other.y - first.y
        a = y_diff / x_diff
        b = first.y - a * first.x
        return (a,b,)

p = Point(0,17)
q = Point(5,27)

print(line_function(p,q))
Logged

kaenneth

  • Bay Watcher
  • Catching fish
    • View Profile
    • Terrible Web Site
Re: if self.isCoder(): post() #Programming Thread
« Reply #2962 on: September 27, 2012, 10:15:08 am »

Only if you also rename 'other' to 'second' =P

or if 'self' is a keyword in your language, like 'this' in C#
Logged
Quote from: Karnewarrior
Jeeze. Any time I want to be sigged I may as well just post in this thread.
Quote from: Darvi
That is an application of trigonometry that never occurred to me.
Quote from: PTTG??
I'm getting cake.
Don't tell anyone that you can see their shadows. If they hear you telling anyone, if you let them know that you know of them, they will get you.

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #2963 on: September 27, 2012, 10:31:36 am »


Yes, I'm aware of this. Working product before shiny product.

On another note, Java is baffling me.

Spoiler: GameButton.java (click to show/hide)
Spoiler: GameGUI.java (click to show/hide)
Spoiler: GamePieces.java (click to show/hide)
Spoiler: GUITest.java (click to show/hide)

Everything appears to work fine. The window appears with a button in it, which has the icon represented by black.png. Except, when I try to click it, nothing happens. The various debugging things I put in GameButton.mouseClicked and GameGUI.processClick aren't doing anything. Where am I going wrong?

You have nothing tying the button to the method you want to call. I see you have a mouseClicked(MouseEvent e) method on your button class. If you don't implement the MouseListener interface, that is meaningless. You also have to bind the button object to an instance of the MouseListener. If your GameButton class implements the  interface MouseListener, the button can be its own MouseListener, and you can bind itself to itself in its constructor. I have modified the code of your GameButton class for this. Note that you could alternatively used an ActionListener.

AKA: Yo dog, I heard you like to MouseListen, so I put a MouseListener in your MouseListener, so you can MouseListen while you MouseListen.
« Last Edit: September 27, 2012, 10:33:23 am by Nadaka »
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2964 on: September 27, 2012, 01:24:04 pm »


Yes, I'm aware of this. Working product before shiny product.

On another note, Java is baffling me.

Spoiler: GameButton.java (click to show/hide)
Spoiler: GameGUI.java (click to show/hide)
Spoiler: GamePieces.java (click to show/hide)
Spoiler: GUITest.java (click to show/hide)

Everything appears to work fine. The window appears with a button in it, which has the icon represented by black.png. Except, when I try to click it, nothing happens. The various debugging things I put in GameButton.mouseClicked and GameGUI.processClick aren't doing anything. Where am I going wrong?

You have nothing tying the button to the method you want to call. I see you have a mouseClicked(MouseEvent e) method on your button class. If you don't implement the MouseListener interface, that is meaningless. You also have to bind the button object to an instance of the MouseListener. If your GameButton class implements the  interface MouseListener, the button can be its own MouseListener, and you can bind itself to itself in its constructor. I have modified the code of your GameButton class for this. Note that you could alternatively used an ActionListener.

AKA: Yo dog, I heard you like to MouseListen, so I put a MouseListener in your MouseListener, so you can MouseListen while you MouseListen.

Ahh, the issue is that I forgot to call addMouseListener. Implementing MouseListener is unnecessary, because JButton already does it.

I'm eating my words now. Disregard that bit, silly casting.
« Last Edit: September 27, 2012, 01:26:36 pm by Mego »
Logged

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2965 on: September 27, 2012, 01:32:55 pm »

If you need just click, why don't you add an actionlistener?

That's fairly reliable. And also help doubleclicks.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2966 on: September 27, 2012, 02:54:31 pm »

Your version doesn't find largest prime factors larger than sqrt(n), since the algorithm stops before there. If you'd let it continue, it would take ages.
The way the 2nd algorithm works it has already removed all prime factors below sqrt(n) from n, so it's impossible for there to be any prime factors left above n except n itself.
I'm talking about the first algorithm. You'll notice that I wrote the second one myself.
Logged

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2967 on: September 27, 2012, 03:57:45 pm »

Your version doesn't find largest prime factors larger than sqrt(n), since the algorithm stops before there. If you'd let it continue, it would take ages.
The way the 2nd algorithm works it has already removed all prime factors below sqrt(n) from n, so it's impossible for there to be any prime factors left above n except n itself.
I'm talking about the first algorithm. You'll notice that I wrote the second one myself.

Yeah, I certainly have a lot to learn and practice:
Spoiler (click to show/hide)
Logged

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2968 on: September 27, 2012, 04:09:31 pm »

There is even another step, not using math at all because you can just copy a solution from the web
Logged

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2969 on: September 27, 2012, 06:00:29 pm »

There's that, but I'd like to be able to produce more clever solutions to problems rather than learning about them afterward or having someone else do the thinking, and even so, the site whose problems I'm working on discourages copypasta:

Quote
Q. I solved it by using a search engine, does that matter?

A. Making use of the internet to research a problem is to be encouraged as there could be hidden treasures of mathematics to be discovered beneath the surface of many of these problems. However, there is a fine line between researching ideas and using the answer you found on another website. If you photocopy a crossword solution then what have you achieved?

And you can't see other people's solutions for a problem there, until you answer it correctly.  The reduce(lcm, xrange(2, 21)) solution is just so much better than mine that I wish I'd thought of it.
Logged
Pages: 1 ... 196 197 [198] 199 200 ... 796