Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
HELP ON TURING- PERFECT SQUARE
Author Message
dinktsr

Posted: Mon Apr 26, 2004 5:05 pm   Post subject: HELP ON TURING- PERFECT SQUARE

i struggle so much at this it's not even funny...
i need a program that keep inputting integers until a perfect square (ex. 81, 9) between 40 and 100

Tony

Posted: Mon Apr 26, 2004 5:08 pm   Post subject: (No subject)

eh? what seems to be the problem?
 code: var num:int loop put "what number?" get num exit when round(sqrt(num))**2 = num end loop put "the end"
Tony's programming blog. DWITE - a programming contest.
Paul

Posted: Mon Apr 26, 2004 5:17 pm   Post subject: (No subject)

wha...?
here's how I would do it, it might be sucky, but i always like to showoff my newly learned knowledge (strintok)
 code: for a: 40..100 if strintok (realstr(sqrt (a),0))then put a exit else put a, " ".. end if end for
Tony

Posted: Mon Apr 26, 2004 5:18 pm   Post subject: (No subject)

hah, while we at it, might also use if sqrt(num) mod 1 = 0 to test for a perfect square
Tony's programming blog. DWITE - a programming contest.
Cervantes

Posted: Mon Apr 26, 2004 5:29 pm   Post subject: (No subject)

jeeze, thats the way i did it. I made the prog but then just as i was about to post my "do your own homework" rant followed by "this is how in english, not in code", I scroll down and there's tony's very blatant answer staring at me.

anywho, I don't understand this bit.
 code: exit when round(sqrt(num))**2 = num

if you take the sqrt of a perfect square and you get an integer. round it and you get an integer. square it and you get the first number
but take the sqrt of a non-perfect square number and you get a num with decimals, round that and you get an integer, square that and you get the number you started with, yes? but obviously if you input 10 into your prog it doesn't exit, but if you input 9 it does.
Tony

Posted: Mon Apr 26, 2004 5:38 pm   Post subject: (No subject)

Cervantes - since round drops the decimal point, when you square the number it will not be the same as the one you squared.

It is basically a test to see if the squareroot of a number is an integer (perfect square) or not. That's why I later posted with mod 1 method, as non integer root will have a .something remainder.

10 (non perfect square) sqrt = 3.16
round(3.16) = 3
3 * 3 = 9
9 != 10

Tony's programming blog. DWITE - a programming contest.
Cervantes

Posted: Mon Apr 26, 2004 5:49 pm   Post subject: (No subject)

i have an incredibly strong urge to edit my above post and erase all my moronic ramblings
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 7 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: