AutoHotkey Community

It is currently May 27th, 2012, 10:39 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: January 3rd, 2008, 4:54 am 
Offline

Joined: April 17th, 2007, 1:37 pm
Posts: 761
Location: Florida
Will determine if the number you input is prime, or displays it's factors.

Note: CPU - intensive.

Bugs: Glitches out (immediately declares prime) when using 'really big*' numbers.
I haven't programmed in handling for negative or non-whole numbers...

To Do: Add a timer for 'big numbers' estimated completion.

I just did this as a practice exercise / fun, so if anyone wants to improve it, by all means go ahead :) Is there a faster way to do this?
Code:
#NoEnv
#SingleInstance, Force
SetBatchLines, -1

Start:
Factors=
Matches=0
Percent=0
Aborted=
Abort=
InputBox, Number, Prime # Tester, Please enter the number you'd like to test:
If ErrorLevel
   ExitApp
Loops:=Floor(Number / 2)
Progress, r1-100, Factors found:`n`nEsc to abort, Checking %A_Index% of %Loops% (%Percent%`%)
Loop, %Loops%
{
   If Abort
      Break
   Percent:=Round((100 * A_Index / Loops),2)
   Pct:=Floor(Percent)
   Progress, %Pct%, Factors found:`n%Matches%`nEsc to abort, Checking %A_Index% of %Loops% (%Percent%`%)
   If A_Index = 1
      Continue
   If (Mod(Number, A_Index) != 0)
      Continue
   Factors=%Factors%%A_Index%`,
   Matches++
}
Finish:
Progress, off
StringTrimRight, Factors, Factors, 1
If Factors
   MsgBox, The following factors (%Matches% total) of %number% were found:`n%factors%%Aborted%
Else
   MsgBox, %Number% is prime!%Aborted%
GoSub, Start
Return

esc::
Aborted=`n(Search aborted at %percent%`%!)
Abort=1
Return


* 18500000000000000000 will not work, but 18400000000000000000 will.

_________________
[Join IRC!]
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 3rd, 2008, 5:36 am 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8667
Location: Salem, MA
I think you can do
Code:
Loops:=Floor(Sqrt(Number))


but then you have to record two factors every time you find one, then sort, unique the list at the end.

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 3rd, 2008, 5:12 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
There is a long discussion about integer factorization (with a lot of alternative algorithms) in this thread.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group