|
|
|
|
#1 |
|
Post Master General
(Forum King) Join Date: Jun 2000
Location: Seattle, Now Las Vegas
Posts: 6,032
|
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'm Back? |
|
|
|
|
|
#2 |
|
Forum Domo
Join Date: Jan 2004
Location: Everyone, get over here for the picture!
Posts: 4,313
|
I used to be a TI 83+ nerd...
elevatorladyelevatorladyelevatorladyelevatorladyelevatorladylevitateme |
|
|
|
|
|
#3 |
|
Forum King
Join Date: Jul 2001
Location: London
Posts: 6,072
|
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 |
|
|
|
|
|
#4 |
|
Forum Domo
Join Date: Jan 2004
Location: Everyone, get over here for the picture!
Posts: 4,313
|
elevatorladyelevatorladyelevatorladyelevatorladyelevatorladylevitateme |
|
|
|
|
|
#5 |
|
Forum King
Join Date: Aug 2002
Posts: 7,254
|
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] Freedom of speech is the basic freedom of humanity. When you've lost that, you've lost everything. 1\/\/4y 34|<$p4y 1gp4y 33714y, 0d4y 0uy4y? | Roses are #FF0000; Violets are #0000FF; chown -R ${YOU} ~/base The DMCA. It really is that bad. : Count for your life. |
|
|
|
|
|
#6 |
|
Forum King
Join Date: Jan 2002
Location: the nether reaches of bonnie scotland
Posts: 13,375
|
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: |
|
|
|
|
|
#7 |
|
Forum King
Join Date: Aug 2002
Posts: 7,254
|
[edit]Nevermind, stupid question.[/edit]
Freedom of speech is the basic freedom of humanity. When you've lost that, you've lost everything. 1\/\/4y 34|<$p4y 1gp4y 33714y, 0d4y 0uy4y? | Roses are #FF0000; Violets are #0000FF; chown -R ${YOU} ~/base The DMCA. It really is that bad. : Count for your life. |
|
|
|
|
|
#8 |
|
Post Master General
(Forum King) Join Date: Jun 2000
Location: Seattle, Now Las Vegas
Posts: 6,032
|
I'm pretty sure this is what a crappy Ti-83 version would look like. Very unoptimized code
code: I'm Back? |
|
|
|
|
|
#9 |
|
Forum Domo
Join Date: Jan 2004
Location: Everyone, get over here for the picture!
Posts: 4,313
|
DelVars don't need a character return
and don't close your () or you "" gotta love TiBasic elevatorladyelevatorladyelevatorladyelevatorladyelevatorladylevitateme |
|
|
|
|
|
#10 |
|
Nullsoft Newbie
(Moderator) Join Date: Mar 2001
Location: Sheffield, England
Posts: 5,569
|
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: DO NOT PM ME WITH TECH SUPPORT QUESTIONS |
|
|
|
|
|
#11 |
|
has no CT
(Forum King) |
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) |
|
|
|
|
|
#12 |
|
Forum King
Join Date: Aug 2002
Posts: 7,254
|
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] Freedom of speech is the basic freedom of humanity. When you've lost that, you've lost everything. 1\/\/4y 34|<$p4y 1gp4y 33714y, 0d4y 0uy4y? | Roses are #FF0000; Violets are #0000FF; chown -R ${YOU} ~/base The DMCA. It really is that bad. : Count for your life. |
|
|
|
|
|
#13 | |
|
has no CT
(Forum King) |
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: |
|
|
|
|
|
|
#14 |
|
Forum Domo
Join Date: Jan 2004
Location: Everyone, get over here for the picture!
Posts: 4,313
|
yeah, use a while() instead of the Lbl:goto.
Leaving a conditional open is not a good Idea. elevatorladyelevatorladyelevatorladyelevatorladyelevatorladylevitateme |
|
|
|
|
|
#15 | |
|
Nullsoft Newbie
(Moderator) Join Date: Mar 2001
Location: Sheffield, England
Posts: 5,569
|
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: DO NOT PM ME WITH TECH SUPPORT QUESTIONS |
|
|
|
|
|
|
#16 | |
|
Forum King
Join Date: Jan 2002
Location: the nether reaches of bonnie scotland
Posts: 13,375
|
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! Last edited by zootm; 21st December 2004 at 18:34. |
|
|
|
|
|
|
#17 |
|
Forum King
Join Date: Aug 2002
Posts: 7,254
|
...
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... Freedom of speech is the basic freedom of humanity. When you've lost that, you've lost everything. 1\/\/4y 34|<$p4y 1gp4y 33714y, 0d4y 0uy4y? | Roses are #FF0000; Violets are #0000FF; chown -R ${YOU} ~/base The DMCA. It really is that bad. : Count for your life. |
|
|
|
|
|
#18 |
|
Forum King
Join Date: Jan 2002
Location: the nether reaches of bonnie scotland
Posts: 13,375
|
Strictly speaking, they're the same thing, you just weren't showing the magnitudes.
|
|
|
|
|
|
#19 |
|
Forum King
Join Date: Aug 2002
Posts: 7,254
|
True.
Freedom of speech is the basic freedom of humanity. When you've lost that, you've lost everything. 1\/\/4y 34|<$p4y 1gp4y 33714y, 0d4y 0uy4y? | Roses are #FF0000; Violets are #0000FF; chown -R ${YOU} ~/base The DMCA. It really is that bad. : Count for your life. |
|
|
|
|
|
#20 |
|
Post Master General
(Forum King) Join Date: Jun 2000
Location: Seattle, Now Las Vegas
Posts: 6,032
|
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. I'm Back? |
|
|
|
![]() |
|
|||||||
| Thread Tools | Search this Thread |
| Display Modes | |
|
|