Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 21st, 2003, 12:45 PM
vb.net vb.net is offline
Demonic Swordsman DGQB
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Apr 2003
Posts: 1,009 vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 14 h 42 m 21 sec
Reputation Power: 77
how does this algorithm work?

First, please pardon my entry-level knowledge. This algorithm determines if a # is prime. I'm mainly confused on the parts involving the square root & the mod operator.

Code:
public static String isPrime(long numToCheck)
	{
		for (long i=2;i<=Math.sqrt(numToCheck);i++)
		{
			if (numToCheck%i==0)
			{
				return "Not prime";
			}
		}
		return "Is indeed prime";
	}

Reply With Quote
  #2  
Old May 21st, 2003, 02:03 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
basically you are just checking to see if any of the numbers smaller than or equal to the square root of your target number can be divided into your number without a remainder. The % (mod) operator returns the remainder of the division operation. So, in this exampe if our target # was 25, we would run the loop for (i = 2 -> i = 5) each iteration of the loop, we divide our target # 25 by the current value of i and check to see if there is a remainder. you can see that wen i gets to 5, the if condition will evaluate to true, since 5 % 5 == 0.

Reply With Quote
  #3  
Old May 21st, 2003, 11:15 PM
vb.net vb.net is offline
Demonic Swordsman DGQB
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Apr 2003
Posts: 1,009 vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 14 h 42 m 21 sec
Reputation Power: 77
I see what's going on. After 1 year of basic programming, I finally see mathematical thinking involved in a program.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > how does this algorithm work?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT