Find equal pentagonal and square number
21 views (last 30 days)
Show older comments
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
Jan
on 14 Sep 2021
@DGM: "when you're one person with 30-45 minutes to walk 20 distracted freshmen through a crash course in writing a couple simple MATLAB scripts"
I do agree. In this situation clear all is not the core problem, but "crash course in 45 minutes". If you have a very short time only to teach the basics, I still think it is worth not to teach anything, which must be taken back in the following hours. If I want to save time, I'd omit to mention scripts and claim that a Matlab file starts with keyword "function ...".
Accepted Answer
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
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))
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.
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!