MATLAB Answers

Find equal pentagonal and square number

5 views (last 30 days)
Shoandeep Radhakrishnan
Shoandeep Radhakrishnan on 9 Sep 2021
Commented: DGM on 14 Sep 2021
A pentagonal number is defined by p(n) = (3*n^2 – n)/2, where n is an integer starting from 1. Therefore, the first 12 pentagonal numbers are 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176 and 210.
A square number is defined by s(n) = n2, where n is an integer starting from 1. Therefore, the first 12 square numbers are 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, and 144.
From the above example, the number 1 is considered 'special' because it is both a pentagonal (p=1) and a square number (s=1). Determine the 3rd 'special' number (i.e. where p=s) assuming the number 1 to the be first 'special' number.
Does anyone knows how to solve this cause i am lost
  6 Comments

Sign in to comment.

Accepted Answer

Jan
Jan on 9 Sep 2021
This sounds like a homework question. Then the standard way is that you post, what you have tried so fas and ask a specific question.
You can create a list of the pentagonal numbers for the first million natural numbers:
n = 1:1e6;
n2 = n .^ 2;
n5 = (3 * n2 - n) / 2;
Now find the elements which occur in both vectors. See:
doc intersect
doc ismember

More Answers (2)

DGM
DGM on 9 Sep 2021
Edited: DGM on 9 Sep 2021
This is a rather brute force method, but it still only takes 1.5ms to find the third match on my dumpster PC.
n = 1:10000;
p = (3*n.^2 - n)/2;
s = n.^2;
p(ismember(p,s))
ans = 1×3
1 9801 94109401
I imagine a symbolic solution might be more elegant, but I'll leave that to someone who uses the symbolic toolbox more than I do.

Jan
Jan on 10 Sep 2021
Edited: Jan on 10 Sep 2021
And a two-liner:
n = 1:10000;
find(any(((3*n.^2 - n)/2) == (n.^2).'))
Matlab can be very elegant. Do you understand the details of this solution?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!