5 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

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

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.

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

Start Hunting!