Finding square roots

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator: Moderators

ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Finding square roots

Post by ghosstt »

Hey. I need the best way to find square roots WITHOUT a calculator.
benol
Posts: 1371
Joined: Thu Aug 31, 2006 1:53 pm
Location: In ur fourmz, postin in ur thredz!
Contact:

Post by benol »

Why? Need to take a no calculators test? Memorization is your best bet.
PM me for DS friend codes. PLEASE!
DOLGAT IS REAL!!!!

Help My MyMiniCity, plzkthxbai
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

no, its for a project im doing. (program) i need a formula or algorithm that will let me pop a number in, and it will tell me if its prime or not.. whats the best method? (quickest)
vskid
Senior Member
Posts: 6314
Joined: Fri Mar 25, 2005 8:25 am
Steam ID: vskid3

Post by vskid »

How would finding the square root help you find out if a number is prime or not? :?
You could just have the program divide the number in question by all the whole numbers before it (except itself and 1), and if it doesn't get a whole number as the answer for any of them, then its prime.
Theres probably an easier/faster way, but thats my only idea. :wink:
Image
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

well, the square root thing was for a previous idea.

The idea you have is what we did in school, though very inefficient for larger numbers..
vskid
Senior Member
Posts: 6314
Joined: Fri Mar 25, 2005 8:25 am
Steam ID: vskid3

Post by vskid »

ghosstt wrote:The idea you have is what we did in school, though very inefficient for larger numbers..
How large? If they're less than 1,000, it'd probably be a good idea to just print out a list of the prime numbers up to 1,000. If you have a graphing calculator, I'm sure you'll be able to find a program that could tell you if a number is prime (check ticalc.org if its a TI).
Image
Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

Code: Select all

int PrimeTest(int x)
{
	x = abs(x);

	if(!x)
	{
		return 1;
	}

	for(int i = 2; i <= sqrt(x); i++)
	{
		if(x % i == 0)
		{
			return 0;
		}
	}
  
	return 1;   
}
Returns 1 if prime, 0 if not.
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

wtf is that.. C++?

im using VB.net btw
Last edited by ghosstt on Fri Oct 19, 2007 4:38 pm, edited 1 time in total.
Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

A function.
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

edited post..

how could i apply that.. thats the

i

part of it?
Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

What? Practically every language supports for loops. The i is an increasing integer that is used to assist in checking if x is divisible by a whole number.
Kyo
Senior Member
Posts: 2470
Joined: Tue Jan 02, 2007 11:59 am
Location: Germany

Post by Kyo »

a variable storing the number of loops the loop has gone thru.
Come on, calling that i is like, the first thing you learn when coding.


edit:

does VB even have for?
Last edited by Kyo on Fri Oct 19, 2007 4:42 pm, edited 2 times in total.
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

I have no clue how to read that code.. all i know is vb.net.. :(
Kyo
Senior Member
Posts: 2470
Joined: Tue Jan 02, 2007 11:59 am
Location: Germany

Post by Kyo »

Well, try to translate it. I mean that code is pretty straight forward...
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

if factorial of x then its prime? what?
Post Reply