views:

142

answers:

7

Suppose two integers x and N.

I'm trying to determine how to construct an algorithm that would return an integer of the value x repeated N times.

So if x was 9 and N was 4, the equation would return 9999.
And if x was 9 and N was 5, the equation would return 99999. (ad nauseam)

I hope this isn't completely absurd or out of place on SO. :)

A: 

Pseudo code to follow. The essence of this is you are going to count up to n and every time you count you will print out your x

for(int i=1; i <=x ; i++)
{
 system.print("n");
}
george9170
this isn't exactly "constructing" the integer, it's just printing a string, which may not be what the OP wants
Armen Tsirunyan
@armen very true. I shoudlve read the question better
george9170
Not to mention that the code is incorrect :)
Armen Tsirunyan
Its psuedo code to express an idea, not meant to be compileable
george9170
it is incorrect in the sense that it must loop till N not x, and print x, and definitely not "n" or even "x"
Armen Tsirunyan
+1  A: 

pseudocode:

Procedure Construct takes x:integer N:integer
begin
   take a variable Result and initialize with 0;
   For N times Do
   begin
      Result <- Result * 10
      Result <- Result + x
   end
end

a C++ example:

int main()
{
   const int x = 9, N = 5;
   int Result = 0;
   for(int i = 0; i < N; ++i)
   {
      Result*=10;
      Result+=x;   
   }
   //use result here
}
Armen Tsirunyan
@armen tsirunyan little complicated for what is needed.
george9170
@armen after reviewing what is needed, i feel that your psuedocode is a much better example.
george9170
@george9170: why thank you :)
Armen Tsirunyan
+2  A: 

This seems like more of a programming question, as the solution is heavily dependent on the base-10 number system. The algorithm to do this would just be a simple loop over N which multiplies the previous number and adds a x.

int foo(int x, int N) {
  int result = 0;
  for(i=0; i<N; i++) {
    result *= 10;
    result += x;
  }
  return result;
}
anonymous_21321
this got me thinking on the right path! thanks :)
Matt H.
Actually Dialecticus's solution below is better, create an integer that is all 1s and N long, product that with x.
anonymous_21321
+1  A: 

Just to be a little different, I've made a JavaScript fiddle with this recursive function:

function repeating(x, n){
    return (n) ? (x * Math.pow(10,n-1)) + repeating(x, n-1) : 0;
};

Fiddle: http://jsfiddle.net/SZKeb/2/

It just works backwards from N, so essentially will calculate as 9000 + 900 + 90 + 9 + 0 = 9999

Jonathon
+10  A: 

This works for me: (10^N-1)/9*x

Dialecticus
+1, beat me by a minute!
Peter G.
@Dialecticus: +1: Cool :)
Armen Tsirunyan
What if N is 11 and x is 12? This equation would give 133,333,333,332.
Thorin
The solution is in my answer, all the way down;)
Michał Trybus
@Michał Trybus: Nice addendum, and depending on the context it might be what OP was looking for. I was however aiming for Laconian badge: good answer in less than 16 characters :-)
Dialecticus
Wow, I didn't know such badge existed:D
Michał Trybus
+2  A: 

Note that x is an integer and it does not have to be a 1-digit number in base-10 system. What if N = 3 and x = 12? Then the answer should be 121212.

Here is the solution: we need the length p of the number x in base-10 system. Let p = floor(lg(x)+1). The number we are searching for is x + x*10^p + x*10^2p + ... + x*10^(N-1)p. That is x * (10^(pN) - 1) / (10^p - 1).

Michał Trybus
A: 

Sounds more like you're trying to construct a string of repeating numbers than doing actual math. Why not do the following (C#)?

using System;
using System.Text;

public int CreateInt(int x, int N)
{
    StringBuilder createdString = new StringBuilder();
    int createdInt;

    for (int i = 0; i < N; i++)
        createdString.Append(x.ToString());

    if (!int.TryParse(createdString.ToString(), out createdInt))
        throw new Exception(string.Format("Value x ({0}) repeated N ({1}) times makes {2}.  This is not a valid integer.", x, N, createdString));

    return createdInt;
}

int createdInt1 = CreateInt(7, 5);  // output: 77777
int createdInt2 = CreateInt(14, 4); // output: 14141414
int createdInt3 = CreateInt(1, 20); // output: throws exception "Value x (1) repeated N (20) times makes 11111111111111111111.  This is not a valid integer."

This sample shows a couple of things you'll wanna watch out for:

  1. Is the created result a valid integer for whatever language you're programming in?
  2. What if the integer to repeat (x) is double digits or higher?
Thorin