I'm trying to write a function that will take an array or vector and have its values taken to a "power of" and then display it's values. I'm not too familiar with arrays but simply put I'm trying to create something like

n = {2^1, 3^1, 5^1,2^2,3^2,5^2,....}

the "power of" is going to be looped. I then plan to sort the array, and display 1500th term.

this problem corresponds to prime number sequence only divisible by 2 , 3 & 5; I'm trying to find a more time efficient way than just if statements and mod operators.

有帮助吗?

解决方案

If I remember correctly this is the Ugly Numbers problem I've faced some years ago in the UVa.

The idea to solve this problem is to use a priority queue with the numbers 2, 3 and 5 as initial values. At each step remove the topmost value t and insert the values 2*t, 3*t and 5*t in the priority queue, repeat this steps till the 1500th term is found.

See this forum for more info: http://online-judge.uva.es/board/viewtopic.php?t=93

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top