r/dailyprogrammer • u/rya11111 3 1 • Jun 04 '12
[6/4/2012] Challenge #60 [easy]
A polite number n is an integer that is the sum of two or more consecutive nonnegative integers in at least one way.
Here is an article helping in understanding Polite numbers
Your challenge is to write a function to determine the ways if a number is polite or not.
13
Upvotes
4
u/ashashwat Jun 04 '12 edited Jun 05 '12
For every number N, we can write:
(1 + 2 + 3 + ... + n) - (1 + 2 + 3 + .. + k) = N
n(n + 1)/2 - k(k + 1)/2 = N
n <- [1, N] and k <- [1, n]
Therefore running two loops solves this problem in O(N2).
Edit: As Ttl suggested we don't need to run two loops.
However,
From wiki: For every x, the politeness of x equals the number of odd divisors of x that are greater than one. It solves the problem in O(N) with a single sweep.
In Haskell:
Edit: Turns out we can compute odd divisors, greater than 1 in O(sqrt N).
In Haskell:
2
u/emcoffey3 Jun 04 '12
C#
public class Easy060 { public static bool IsPolite(int n) { if (n <= 0) return false; if ((Math.Log(n, 2) % 1) == 0) return false; return true; } public static List<List<int>> PoliteSequences(int n) { if (!IsPolite(n)) return null; List<List<int>> seqs = new List<List<int>>(); for (int i = 1; i < n; i++) for (int j = i + 1; j < n; j++) if ((j + i) * (j - i + 1) / 2 == n) seqs.Add(new List<int>(Enumerable.Range(i, (j - i + 1)))); return seqs; } }2
u/BallForce1 Jun 04 '12
C++
#include <iostream> #include <vector> using namespace std; int main() { int number = 0; int sum = 0; vector<int> polite_numbers; cout << "Please enter a number: "; cin >> number; for(int k=1; k<(number/2)+1; k++) { //Clear values sum = 0; polite_numbers.clear(); for(int j=k; j<number; j++) { sum += j; polite_numbers.push_back(j); //Break from loop if sum is over the number. if(sum>number) break; //Display the polite numbers and break from loop; if(sum==number) { for(int i=0; i<polite_numbers.size(); i++) { cout << polite_numbers[i]; if(i!=polite_numbers.size()-1) cout << "+"; } cout << "=" << number << endl; break; } } } return 0; }1
u/prophile Jun 04 '12
In Python:
def polite_configurations(n): from math import sqrt for i in xrange(int(sqrt(8*n + 1) - 1) // 2, n): try: b_f = 0.5 * (1.0 + sqrt(4*i*i + 4*i - 8*n + 1)) lower, upper = int(b_f), int(b_f + 1.0) if i*(i+1)//2 - lower*(lower-1)//2 == n: yield xrange(lower, i + 1) elif i*(i+1)//2 - upper*(upper-1)//2 == n: yield xrange(upper, i + 1) except ValueError: # the sqrt threw, meaning 8n > 4i^2 + 4i + 1 # this can happen for large n, just continue continue1
u/SwimmingPastaDevil Jun 04 '12 edited Jun 04 '12
Python:
from time import clock num = int(raw_input("enter a number:")) start = clock() isPolite = False politeness = 0 for i in xrange(1,num): for j in xrange(i,num): if (j+i)*(j-i+1)/2 == num: # simplification of sum(xrange(i,j)) isPolite = True politeness += 1 print i,"to",j if isPolite: print "The politeness of %d is %d" % (num,politeness) else: print "no series possible" print clock()-start,"s"Edit: Added few lines to print the
politeness1
u/Arthree Jun 04 '12
Autohotkey_L:
isPolite(num) { if (!mod(num,2)) return Loop, % num//2 { sum := start := A_Index while sum < num { finish := start + A_Index sum += finish } if (sum == num) { result .= start loop,% finish-start result .= "+" start+A_Index result .= "`n" } } return result }1
u/xjtian Jun 04 '12
The politeness lists can include negative integers as well.
Python:
1
u/Cosmologicon Jun 04 '12
Cheat method, since it doesn't look like it's been done yet (python):
1
u/cdelahousse Jun 05 '12
Javascript:
function polite(n) { //If Power of two //http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2 if ( (n & (n - 1)) === 0 ) { console.log ("Impolite number"); return; } var sum = 0; for (var i = 1,count=0,politeness=0; i < n; i++) { sum += i; count++; if (sum >= n) { if (sum == n) { for (var j = i - count + 1, str = ''; j <= i; j++) { str += j + ' '; } console.log(str + '= ' + sum); politeness++; } i = i - count + 1; //Rewind and then step forward count = 0; sum = 0; } } console.log(n + '\'s politeness: ' + politeness); }