![]() |
Prime Factors
I'm trying to make a program for my calculator that will figure out Prime Factors of a particular number but I am having strangely the hardest time getting one to work. I have one that figures out all the Prime numbers between a certain integer range but its been so long since I wrote anything.
Anyways, Who can do what I seemingly can't do? C++, Pascal, Java, Delphi, JavaScript, Basic whatever you feel like working with. Should be a simple exercise to pass the time. |
I used to be a TI 83+ nerd...
|
First use your prime number generator to check it's not already prime.
Then succcessively take the numbers from your PNG and test for exact division using something like MOD=0, if true use DIV to determine the remaining dividend and if > 1 apply the same process again (starting with the largest number you reached on the previous calc.) collect your results together. You only need to use values up to SQR(n). Or something like that :) UJ |
|
Yay! Stupid/fun coding project:
code: [edit]Note: does not print "1" as a prime factor. If you'd like it to, simply: change "i > 1" to "i > 0" in PrintPrimeFactors().[/edit] |
Apparently 1 isn't strictly a prime number. Which is good, because in what I was gonna do, it would lead to an infinite loop. Since xzxzzx has basically done it, I'll just base my implementation roughly on his...
code: |
[edit]Nevermind, stupid question.[/edit]
|
I'm pretty sure this is what a crappy Ti-83 version would look like. Very unoptimized code
code: |
DelVars don't need a character return
and don't close your () or you "" gotta love TiBasic |
code: This implementation in MosML is clearly the best. The function facts() takes a number and gives a list of its prime factors. [edit] here is a java version that works exatly the same, but is much less tearse. [/edit]code: |
Y'all made this much more difficult than it is. This is how I have it on my calc (I'm doing from memory, so systen may be a bit off, but you'll get the idea)
code: I'll look more closely @ the code when I get my calc back (my g/f has it) |
Whaa? I don't know TiBasic, dlinkwit27, but I don't think that'd do what whiteflip wants.
[edit]Have you tested that Java, will? It doesn't look like it'd work to me.[/edit] |
Quote:
I should change the for loop tho. The for loop should be int(p/2) instead of p-1\ /edit crap. I see wht the problem is. I failed to reduce the number you start with. Let me change that.... again, the syntax may be a bit off, but you should be able to figure it out. code: |
yeah, use a while() instead of the Lbl:goto.
Leaving a conditional open is not a good Idea. |
Quote:
For example, given the input 2132 it gives the output: 2 2 13 41 and, of course 2*2*13*41 = 2132. The given number is x. minFactor starts at 2, the smallest prime number. If the number has a factor of 2, it adds it to the list and does the same for x/2. If there is no factor of 2, then it tries 3, then 4. 4 is not prime, so this test is redundent because all factors of 2 have been taken out already, so it quickly skips to the next. Here is an iterative version that may be easier to understand. code: |
Quote:
Edit: And that's how I was gonna do the system, although I was gonna start it at 1 (thus the infinite loop). Wasn't thinking straight! |
...
So can't remember my math. My program isn't what one is looking for - I was just printing all "factors" (using the idiot definition) that happened to be prime, not the Prime Factors... |
Strictly speaking, they're the same thing, you just weren't showing the magnitudes. :)
|
True.
|
If I could only use my palm pilot on test. The calculator I downloaded for it is so awesome. Yeah I could do Delvar X,Y,Z but I grew up with the other way. My memory cleanup program has it the more efficent way.
I have no idea how to get a program to the point of 2,3^5,7 so oh well. |
| All times are GMT. The time now is 20:28. |
Copyright © 1999 - 2010 Nullsoft. All Rights Reserved.