#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Finding perfect squares between a range of input by user.


    Write a C program that finds the sum of the first two square
    numbers between any given range of whole numbers..Example
    of Output:
    Range: 2 to 12
    First two square numbers: 4 and 9
    Sum of the squares: 13
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    u should not ask for ready code.
    first give a try and then if u face any problem then you should ask here.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,702
    Rep Power
    480
    With appropriate definitions, a j solution is
    Code:
       sum At (2 take isSquare Filter) At range 2 12
    13
    Code:
    At=: @:
    Filter=: (#~`)(`:6)
    Under=: &.:
    difference=: -/
    equal=: =
    integers=: i.
    isSquare=: equal roundDown Under squareRoot
    minimum=: <./
    plus=: +
    range=: minimum plus integers At difference
    roundDown=: <.
    squareRoot=: %:
    sum=: +/
    take=: {.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    and for the Schemers:
    Code:
    (define (is-square? x) (= (* (sqrt x) (sqrt x)) x))
    (define (get-square min-val max-val) 
      (cond
        [(> min-val max-val) false]
        [(is-square? min-val) min-val]
        [else (get-square (+ min-val 1) max-val)]
        ))
    ... rest of code left for user to complete ...
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,702
    Rep Power
    480
    And get-square looks tail recursive. Please, is it so?
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    Yes sir, it is tail-recursive :)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,702
    Rep Power
    480
    Thank you! Clearly, 30 years of occasionally thinking about lisp has paid me a big dividend!

    (I have read and understood this code.)
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo