Find equal pentagonal and square number
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
Steven Lord
on 9 Sep 2021
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Shoandeep Radhakrishnan
on 9 Sep 2021
Edited: Jan
on 10 Sep 2021
The brute clearing header clear all; close all; clc; is not useful. Many teachers seem to mention this, because they have seen in in their classes 20 years ago. But I cannot understand why to teach something, which should never occur in professional code.
Your apporach is fine. The definition of S does not match the formula. n and r are identical, so you can omit one of them. Use the counter:
counter = 0;
result = [];
for i = 1:length(n)
space = find(h(i) == m);
if ~isempty(space)
counter = counter + 1;
result(counter) = length(space);
end
end
DGM
on 14 Sep 2021
I hate to seem contrarian in what's a tangent, but 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, brute clearing of the workspace and outputs solves about half of all the confusion that comes up and reduces the need to be in multiple places at once. While it might be a classroom convenience that's often taken too much as part of the lesson, I know now that there are other bad practices which are taught for sake of their apparent simplicity. In the end, most coursework has only the faded scent of real-world practice. At some point, the distinction needs to be acknowledged and learning must continue.
For what it's worth, that's my experience, but it wasn't my idea to start teaching it that way either. Just sayin that maybe that's the motivation.
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 ...".
DGM
on 14 Sep 2021
I can agree with that.
Accepted Answer
More Answers (2)
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.
1 Comment
Shoandeep Radhakrishnan
on 9 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?
1 Comment
Shoandeep Radhakrishnan
on 14 Sep 2021
Categories
Find more on Mathematics 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!