Find equal pentagonal and square number

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

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.
So this is what I am getting as of now for my code
clear all; close all; clc;
%P = @(n) (3.*n.^2-n)./2;
%S = @(n) n.^2;
P = @(n) n.^2 ; %function handle
S = @(n) n.^3;
n=1:100;
r=1:100;
m=P(n);
h=S(r);
counter=0;
for i = 1:length(n)
space = find(h(i)==m); %
index(i)=length(space)
end
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
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.
@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 ...".
I can agree with that.

Sign in to comment.

 Accepted Answer

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?

Categories

Find more on Mathematics in Help Center and File Exchange

Commented:

DGM
on 14 Sep 2021

Community Treasure Hunt

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

Start Hunting!