Challenge: lucky numbers | Sololearn: Learn to code for FREE!

+5

# Challenge: lucky numbers

Lucky numbers are "survivors numbers" in an array once a specific pattern is recursively applied (numbers in array are removed at every recursion cycle). every recursion cycle has to remove numbers from position "p" and every position interval that is the number in starting position. note that in every cycle, p is one bigger than p in previous cycle for example, lucky numbers from 1 to 120 are (1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, 87, 93, 99, 105, 111, 115) task: write a code - in every language you prefer - that displays all lucky numbers from 1 to user input p.s.: as explaining this in a few characters is difficult, i post here wikipedia link of the full explanation with examples: https://en.wikipedia.org/wiki/Lucky_number good coding :)

8/26/2017 11:18:38 AM

Luca

12 Answers

New Answer

+2

@baptiste you don't have to consider number in position p in the original array, but in the newborn array after previous pass... fourth pass is not with number 5, but number in fourth position in the array resulting from third pass edit: sorry, i red your answer after i posted :D glad you got it :)

+2

https://code.sololearn.com/WJw7SO358XDv/?ref=app

+1

Your example is not the output for 25 but the output after the 3rd pass ... But aren't there more pass ? Or should we stop to 3 ? Regardless, here is my try https://code.sololearn.com/cGE3ca1WE1GW/?ref=app

+1

@baptiste this is because both the output of third pass and for 25, as in such short sequence only 3 passes are done. the fourth pass would be every ninth enumber starting from ninth, buth there are only 8 left numbers in array... this is my php code for lucky numbers https://code.sololearn.com/wrN1AdPdTVmf/#php limit is set to 120, and as you can see it goes over 3rd passage... my fault, i gave too fiew numbers as example, i will edit soon :)

+1

@baptiste i checked your code, but i get all integers from 1 to input...

+1

@Luca, I understand now ! I corrected my code accordingly (I had the increment of the nth pass wrong)

+1

mine https://code.sololearn.com/WWYi4sT7BEKV/?ref=app

0

I may have misunderstood something but p should increase each time by one, so on fourth pass, be 5, no ? And the result should be applied on the list without the unlucky members already founded no ?

0

Here you go. Checks for valid input. https://code.sololearn.com/cHVvQwxYDx7d/?ref=app

0

@Luca yes, I forgot the line l.pop(j) but I replaced it, sorry ^^'

0

here is new challenge... https://www.sololearn.com/discuss/664933/?ref=app

-1

TRY THIS BRAND NEW CHALLENGE AND GET FRESH.. https://www.sololearn.com/discuss/666260/?ref=app