d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Mathematica Perfect Square > N(n+1) And N(n+k)
Add Reply New Topic New Poll
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Feb 27 2014 02:41pm
So, in Mathematica, I'm supposed to write a predicate to test various sets for perfect squares.

I wrote perfectSquareQ[n_]:= IntegerQ[Sqrt[n]]
which returns true or false for the given n

Next, I'm supposed to use this to test whether n(n+1) is ever a perfect square for all integers less than 10,000. What I wrote is:
Select[Range[10000], Map[perfectSquareQ, Range[10000](Range[10000]+1)]
It returns an empty set, which I don't know if that's accurate.

Then it asks if n(n+2) will be a perfect square in the first 10000 digits, so I replace the +1 with a +2, and get an empty set again.



Finally, here's where I'm having problems (unless everything else has also been wrong).
It wants me to create a new function with one argument (k) where the output is a list of all numbers n < 10000, such that n(n+k) is a perfect square.
I don't really know how to set this up correctly without using two arguments.

What I tried was:
perfectSquares[k_] := Select[Range[10000], IntegerQ[Sqrt[Range[10000]*(Range[10000]+k)]]]
Then tested it with 5, which should have returned at least {4}, but got an empty set again.

I'm really at a loss as to what I'm doing wrong, so any help is appreciated.

Also, I don't think we're supposed to use loops as I got points off the last assignment for using one (he hasn't taught them yet).

I really appreciate any help!

This post was edited by furbyjs on Feb 27 2014 02:44pm
Member
Posts: 724
Joined: Jan 19 2010
Gold: 9,788.18
Feb 27 2014 03:07pm
Quote (furbyjs @ Feb 27 2014 04:41pm)

Then tested it with 5, which should have returned at least {4}, but got an empty set again.


Why would k = 5 return at least 4?
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Feb 27 2014 03:15pm
Quote (allthelivelongday @ Feb 27 2014 04:07pm)
Why would k = 5 return at least 4?


Because 4(4+5) is 36, a perfect square.
There should be a few like that I guess.

I figured out what I was doing wrong, but now I need to find a way to pull out the numerical values for the trues and falses.

Code
perfectSquares[k_] := Map[IntegerQ, Sqrt[Range[10] (Range[10] + k)]]

test = perfectSquares[5]

{False, False, False, True, False, False, False, False, False, False}


How could I extract from that the number that translates to the true value?

This post was edited by furbyjs on Feb 27 2014 03:17pm
Member
Posts: 28,331
Joined: Jun 9 2007
Gold: 11,700.00
Feb 27 2014 05:15pm
@ op:

not sure i fully understand what you have to do for your assignment, so without further infor can't comment on that
but are you sure that you have the right test for a number being a perfect square?
Member
Posts: 28,331
Joined: Jun 9 2007
Gold: 11,700.00
Feb 27 2014 07:01pm
Quote (furbyjs @ 27 Feb 2014 20:41)
...
I'm supposed to use this to test whether n(n+1) is ever a perfect square for all integers less than 10,000.
...
It returns an empty set, which I don't know if that's accurate.
Then it asks if n(n+2) will be a perfect square in the first 10000 digits, so I replace the +1 with a +2, and get an empty set again.
...


correction to my previous post, read too quickly across the op

n*(n+1) and n*(n+2) are never perfect squares
just consider that a perfect square has the form prod([i=1,n]pi^ki) with the pi being different primes and all ki being even numbers

btw: for n*(n+3) you will just find one example, namely 1*4

This post was edited by brmv on Feb 27 2014 07:02pm
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Feb 28 2014 10:05am
The problem I'm having is pulling out the Trues from a list of 10,000 entries, almost all of which are False.
I'm pretty sure all my functions are correct, I just recently realized that my select statement wasn't working...

/e Basically, I need some way to find out which positions in the lists are True and the corresponding value
i.e. return the number 4 because that value is true in

Code
perfectSquares[k_] := Map[IntegerQ, Sqrt[Range[10] (Range[10] + k)]]

test = perfectSquares[5]

{False, False, False, True, False, False, False, False, False, False}


This post was edited by furbyjs on Feb 28 2014 10:28am
Member
Posts: 28,331
Joined: Jun 9 2007
Gold: 11,700.00
Feb 28 2014 10:31am
Quote (furbyjs @ 28 Feb 2014 16:05)
The problem I'm having is pulling out the Trues from a list of 10,000 entries, almost all of which are False.
I'm pretty sure all my functions are correct, I just recently realized that my select statement wasn't working...
/e Basically, I need some way to find out which positions in the lists are True and the corresponding value
i.e. return the number 4 because that value is true in
Code
perfectSquares[k_] := Map[IntegerQ, Sqrt[Range[10] (Range[10] + k)]]
test = perfectSquares[5]
{False, False, False, True, False, False, False, False, False, False}


why so complicated?
to test for perfect square take the square root and verify that it is an integer (*)

btw, the number 1 is a perfect square

(*) looks you are doing that - why do you need that long list?

This post was edited by brmv on Feb 28 2014 10:44am
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Feb 28 2014 10:52am
Well, to verify if it's an integer, you get a boolean value back.

Is there a better way to pull integers?

When I try:
Select[Range[10], IntegerQ[Sqrt[Range[10] (Range[10] + 5)]]]
I get an empty set, because I'm selecting integers from a list of booleans...
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Feb 28 2014 11:08am
Never mind, it's all working now xD

Thanks for trying to help

This post was edited by furbyjs on Feb 28 2014 11:11am
Go Back To Homework Help Topic List
Add Reply New Topic New Poll