Re: How do you do these?????
Quote:
Originally posted by alejandrman
How would I program these in MS Visual studio .net:
1) DivisibleBy: Receives two positive integers. The first integer must be greater than the second. Return a zero-length string if this is not the case or if the integers are less than zero. Otherwise, the procedure will return a string containing all the numbers between 0 and the first parameter that are divisible by the second parameter with each number separated by a comma. For instance, if the arguments are 20 and 3 then the return string would be "3,6,9,12,15,18".
and this...
2) NumberOfTimes: Receives two strings and returns an integer representing the number of times the first string appears inside the second string. You cannot use the split method of the string object; however, the SubString and IndexOf methods should be quite helpful.
1. 20 is not divisible by three... I think you meant factors of second input that are less than or euqal to first input.
Anyway to get the factors, eg 20 has 2x2x5
a) Repeatedly determine divisibility by (2n-1) where:
n >= 2, start with divisible by 3
Every time the number is divisible by 2n-1, store the value of n
then start next divisibility loop at the stored value of n and update X to X=X/(2n-1)
After doing that, you should get divisibility for all prime numbers excluding 2 eg 3, 5, 7, 11 with repetitions if applicable eg 3x3... note although you'll check at 9 (for simplicity just increment, dont check for factors of 3, 5, etc in 2n -1) it will not be returned as a factor since you already repeatedly dividing by 3 (stored n) until it is no longer divisible by 3 and n is incremented.
b) After doing divisibility by odd, repeatedly divide by 2 to get number of factors of 2.
c) If no factors are returned, then factors are 1 and the number itself
2) If a substring exists and its position determined, remove the string and check again if another substring exists..