5 kyu

How many new friends John has?

96 of 110myjinxin2015

Description:

When no more interesting kata can be resolved, I just choose to create the new kata, to solve their own, to enjoy the process --myjinxin2015 said

Description:

John is the president of the smart code Association. Christmas is coming, he is going to have a party for all the members. All members numbered from 1 to n(n is the number of John). Some members are John's old friends, but also some new friends. John found that all his old friend's number and his own number have at least one common divisor (greater than 1). Please calculate how many new friends John has?

Complete function countNewFriends. Function accept a argument n(a positive integer, 0 < n < 30000). Returns a number that is the total number of John's new friends.

Some Examples

# Counts the number of integers from 1 to n-1 that are coprime with n

# Examples:
# n = 1  ->  0  (No new friends)
# n = 8  ->  3  (New friends: 3, 5, 7)
# n = 9  ->  5  (New friends: 2, 4, 5, 7, 8)
# n = 10 ->  3  (New friends: 3, 7, 9)
# n = 25608 -> 7679  (Large number of friends)
# n = 24027 -> 16015 (Large number of friends)

Note:

  • You can assume that all inputs are valid.
  • The friend which number is 1 is always a old friend of John or it's John himself(when n = 1).

    A bit difficulty, A bit of fun, happy coding ;-)

Algorithms
Puzzles

Stats:

CreatedNov 21, 2016
PublishedNov 21, 2016
Warriors Trained315
Total Skips38
Total Code Submissions256
Total Times Completed110
JavaScript Completions96
Python Completions17
Total Stars14
% of votes with a positive feedback rating93% of 27
Total "Very Satisfied" Votes24
Total "Somewhat Satisfied" Votes2
Total "Not Satisfied" Votes1
Total Rank Assessments5
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • smile67 Avatar
  • dfhwze Avatar
  • saudiGuy Avatar
Ad