Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help understanding what a and n are.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ericnewman




PostPosted: Thu Jan 17, 2008 8:41 pm   Post subject: Need help understanding what a and n are.

for n : 2 .. 10
var a : real := 1
loop
a := a + 1
if a > sqrt (n) then
put n,", "..
exit
end if
exit when n mod a = 0
end loop
end for

What would these variables be, I don't understand how the command "sqrt" works.
Sponsor
Sponsor
Sponsor
sponsor
StealthArcher




PostPosted: Thu Jan 17, 2008 8:42 pm   Post subject: RE:Need help understanding what a and n are.

If this is your code, than how come you dont already know what it does, if it isn't why are you stealing code?
ericnewman




PostPosted: Thu Jan 17, 2008 8:47 pm   Post subject: Re: Need help understanding what a and n are.

I am not stealing code because i am not using it. I just wanted to understand it.
StealthArcher




PostPosted: Thu Jan 17, 2008 8:49 pm   Post subject: RE:Need help understanding what a and n are.

Where is it from then, we've had too many incidences of people wanting their work done for them, who will then copy it and hand it in.

But for your question:
a and n would be integers or reals, It basically states that:
if a's value is over the value of the square root of n then...

square root being: A factor of a number which, when multiplied by itself, equals the number in question.
ericfourfour




PostPosted: Thu Jan 17, 2008 9:13 pm   Post subject: RE:Need help understanding what a and n are.

StealthArcher, calm down. It's 11 lines of code and a very simple algorithm. Calling this stealing code is similar to saying every time someone copies the quicksort algorithm, they are stealing.

sqrt is a function. It returns the square root of a specified number.
Nick




PostPosted: Thu Jan 17, 2008 9:38 pm   Post subject: RE:Need help understanding what a and n are.

OH NO!!! I stole the insertion sort algorithm!!!!!

honestly though n is an increasing number started at 2 ending at 10 and a is a number increasing by one every loop run
Sean




PostPosted: Fri Jan 18, 2008 8:02 am   Post subject: Re: Need help understanding what a and n are.

ericnewman @ Thu Jan 17, 2008 8:41 pm wrote:
for n : 2 .. 10
var a : real := 1
loop
a := a + 1
if a > sqrt (n) then
put n,", "..
exit
end if
exit when n mod a = 0
end loop
end for

What would these variables be, I don't understand how the command "sqrt" works.


a is a variable, set as a real number, which has been given the value of one. The n is the counter, starting from 2 and counting up to 10 by 1. The program keeps increasing a by 1 until a is greater then the sqrt of n.
ericfourfour




PostPosted: Fri Jan 18, 2008 3:57 pm   Post subject: Re: Need help understanding what a and n are.

We can also try writing it in a more understandable language like ruby:

Ruby:
2.upto(10) do |n|
  a = 1
  loop do
    a += 1
    if a > Math.sqrt(n)
      print "#{n}, "
      break
    end
    break if n % a == 0
  end
end


Note: I guessed this, so it might not work.
Sponsor
Sponsor
Sponsor
sponsor
StealthArcher




PostPosted: Fri Jan 18, 2008 4:04 pm   Post subject: RE:Need help understanding what a and n are.

Can't tell myself, ruby isn't my forte.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: