Old 19th December 2004, 21:22   #1
whiteflip
Post Master General
(Forum King)
 
whiteflip's Avatar
 
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?
whiteflip is offline   Reply With Quote
Old 20th December 2004, 02:58   #2
shakey_snake
Forum Domo
 
shakey_snake's Avatar
 
Join Date: Jan 2004
Location: Everyone, get over here for the picture!
Posts: 4,313
I used to be a TI 83+ nerd...


elevatorladyelevatorladyelevatorladyelevatorladyelevatorladylevitateme
shakey_snake is offline   Reply With Quote
Old 20th December 2004, 15:47   #3
ujay
Forum King
 
ujay's Avatar
 
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
ujay is offline   Reply With Quote
Old 20th December 2004, 15:51   #4
shakey_snake
Forum Domo
 
shakey_snake's Avatar
 
Join Date: Jan 2004
Location: Everyone, get over here for the picture!
Posts: 4,313
http://www.ticalc.org/search/


elevatorladyelevatorladyelevatorladyelevatorladyelevatorladylevitateme
shakey_snake is offline   Reply With Quote
Old 20th December 2004, 18:07   #5
xzxzzx
Forum King
 
xzxzzx's Avatar
 
Join Date: Aug 2002
Posts: 7,254
Yay! Stupid/fun coding project:
code:
// stupidfun.cpp : Yay!
//

#include <stdio.h>

bool IsPrime(int n)
{
for(int i = (n - 1); i > 1; --i){
if((n % i) == 0) {
return false;
}
}
return true;
}

void PrintPrimeFactors(int n)
{
int x = 1;
for(int i = (n - 1); i > 1; --i){
if(((n % i) == 0) && IsPrime(i)){
printf("A prime factor #%d is: %d\n", x++, i);
}
}
}

int main(void)
{
PrintPrimeFactors(6);
getchar(); // for a pause
return 0;
}



[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.
xzxzzx is offline   Reply With Quote
Old 20th December 2004, 19:19   #6
zootm
Forum King
 
zootm's Avatar
 
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:

// in Java, incidentally.

class Fun {
static boolean isPrime (int n) {
boolean prime = true;
for (int i = 2; i < n; i++) {
if (n%i == 0) {
prime = false;
break;
}
}
return prime;
}

static String getPrimeFactorisationAsString (int n) {
int x = 1;
int j;
String result = "";
for(int i = (n - 1); i > 1; --i) {
if ((n%i == 0) && isPrime(i)) {
for (j = 2; true; j++) {
if (!((n%(Math.pow(i,j)))==0)) break;
}
result += "(" + i + "^" + j-1 + ")";
}
}
if (result.equals("")) result = new String ("(" + n + "^1)");
return result;
}

public static void main (String [] args) {
if (args.length != 1) {
System.out.println ("Please give me a number.");
} else {
int number = Integer.parseInt(args[0]);
System.out.println ("The prime factorisation of " + number + " is " + getPrimeFactorisationAsString(number));
}
}
}


zootm is offline   Reply With Quote
Old 20th December 2004, 19:32   #7
xzxzzx
Forum King
 
xzxzzx's Avatar
 
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.
xzxzzx is offline   Reply With Quote
Old 21st December 2004, 05:31   #8
whiteflip
Post Master General
(Forum King)
 
whiteflip's Avatar
 
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:

ClrHome
DelVar P
DelVar X
DelVar Y
DelVar Z
DelVar L1
DelVar L2
{0}->L2
{1}->L1
DelVar lPrime
Disp "PRIME FACTORS"
Input "INTEGER: ",P
abs(int(P))->P
If P <= 2
Goto 01
For (X,P-1,1,-1)
If P/X=int(P/X)
Then
1->Y
For (Z,1,int(X/2))
If gcd(Z,X)!=1
0->Y
End
If Y=1
Then
Disp X
{X}->L2
If Z!=1
augment(L1,L2)->L1
End
End
End
L1->lPrime
DelVar X
DelVar Y
DelVar Z
DelVar L1
DelVar L2
Disp "PRIME FACTORS OF",P,lPrime
DelVar P
Return
Lbl 01
Disp "INTEGER TO SMALL"
Return


I'm Back?
whiteflip is offline   Reply With Quote
Old 21st December 2004, 05:40   #9
shakey_snake
Forum Domo
 
shakey_snake's Avatar
 
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
shakey_snake is offline   Reply With Quote
Old 21st December 2004, 14:16   #10
will
Nullsoft Newbie (Moderator)
 
will's Avatar
 
Join Date: Mar 2001
Location: Sheffield, England
Posts: 5,569
code:

fun facts(x) = let fun f(1,n) = []
| f(x,n) = if x mod n = 0 then n::f(x div n, n) else f(x, n+1);
in f(x,2) end;


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.
code:
public class PrimeFactors {
public static String findPrimes(int x) {
return doFindPrimes(x,2);
}

private static String doFindPrimes(int number, int minfactor) {
if(number == 1) return "";
if(number % minfactor == 0) return minfactor + " " + doFindPrimes(number / minfactor, minfactor);
return doFindPrimes(number, minfactor + 1);
}
}

[/edit]

DO NOT PM ME WITH TECH SUPPORT QUESTIONS
will is offline   Reply With Quote
Old 21st December 2004, 14:39   #11
dlinkwit27
has no CT
(Forum King)
 
dlinkwit27's Avatar
 
Join Date: Sep 2000
Posts: 13,236
Send a message via ICQ to dlinkwit27 Send a message via AIM to dlinkwit27 Send a message via Yahoo to dlinkwit27
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:

DelVar Q
DelVar R
r=2
DelVar L1
Input "Number: ",P
abs(int(P))->P
Lbl 1

if Q<P-2
then
R=2
For (R,R<P-1,r+1)
if mod(p/r)=0
then
r->L1
goto1
else
end

end


Disp L1
End




I'll look more closely @ the code when I get my calc back (my g/f has it)
dlinkwit27 is offline   Reply With Quote
Old 21st December 2004, 14:47   #12
xzxzzx
Forum King
 
xzxzzx's Avatar
 
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.
xzxzzx is offline   Reply With Quote
Old 21st December 2004, 15:15   #13
dlinkwit27
has no CT
(Forum King)
 
dlinkwit27's Avatar
 
Join Date: Sep 2000
Posts: 13,236
Send a message via ICQ to dlinkwit27 Send a message via AIM to dlinkwit27 Send a message via Yahoo to dlinkwit27
Quote:
Originally posted by xzxzzx
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]
basicly what it does is takes in a number (p) , then divides that number by increaseing amounts starting @ 2 (r) until the remainder is 0. If the remainder is 0, it storest that number (r) in a list, then does it again. I may have used a while loop. I'm not sure.

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:

DelVar L1
Input "Number: ",P
lbl 1
2->D
while(D<int(P/2) and mod(P/D))

D+1>D
end
P/D->P
D->L1
goto 1
End
Disp L1
End

dlinkwit27 is offline   Reply With Quote
Old 21st December 2004, 15:21   #14
shakey_snake
Forum Domo
 
shakey_snake's Avatar
 
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
shakey_snake is offline   Reply With Quote
Old 21st December 2004, 15:39   #15
will
Nullsoft Newbie (Moderator)
 
will's Avatar
 
Join Date: Mar 2001
Location: Sheffield, England
Posts: 5,569
Quote:
Originally posted by xzxzzx
Have you tested that Java, will? It doesn't look like it'd work to me.
If by working you mean take a number and give a list of prime factors in numerical order then yes.
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:
public static String findPrimesIter(int x) {
int minfactor = 2;
String ret = "";
while(x > 1) {
if(x % minfactor == 0) {
ret += minfactor + " "; // add this factor to the list
x /= minfactor; // remove this factor from x now it is the list
}
else ++minfactor; // no more factors of this, so try the next.
}
return ret;
}


DO NOT PM ME WITH TECH SUPPORT QUESTIONS
will is offline   Reply With Quote
Old 21st December 2004, 18:16   #16
zootm
Forum King
 
zootm's Avatar
 
Join Date: Jan 2002
Location: the nether reaches of bonnie scotland
Posts: 13,375
Quote:
Originally posted by will
This implementation in MosML is clearly the best. The function facts() takes a number and gives a list of its prime factors.
SML is the best language ever, yeah

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.
zootm is offline   Reply With Quote
Old 21st December 2004, 20:22   #17
xzxzzx
Forum King
 
xzxzzx's Avatar
 
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.
xzxzzx is offline   Reply With Quote
Old 21st December 2004, 21:03   #18
zootm
Forum King
 
zootm's Avatar
 
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.

zootm is offline   Reply With Quote
Old 21st December 2004, 21:07   #19
xzxzzx
Forum King
 
xzxzzx's Avatar
 
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.
xzxzzx is offline   Reply With Quote
Old 21st December 2004, 21:42   #20
whiteflip
Post Master General
(Forum King)
 
whiteflip's Avatar
 
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?
whiteflip is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Community Center > General Discussions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump