Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Very simple but why doesn't it work?

  1. Jan 12, 2006 #1
    Very simple...but why doesn't it work!?

    The program should output every number of a user-input base, containing a certain quantity of digits.
    import java.io.*;

    public class BaseCount {

    . public static void main(String args[]) {

    . int base = 0, dgts = 0;

    . int[] bsq;

    . BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    . try {
    . System.out.println("Enter a natural base:");
    . base = Integer.parseInt(br.readLine ());

    . System.out.println("Enter the maximum number of digits:");
    . dgts = Integer.parseInt(br.readLine ());
    . }//try
    . catch(IOException e) {}

    . bsq = new int[dgts];

    . for(int i=0; i<Math.pow(base,dgts); i++) {

    . for(int k=0; k<dgts; k++) {
    . bsq[k] = i / (int) Math.pow(base,k) % base;
    . System.out.print(bsq[dgts-k-1]);
    . }//for

    . System.out.println();

    . }//for

    . }//main

    However, there seem to be problems! :frown:

    For example, let base = 2 and dtgs = 4.
    The program outputs:

    Code (Text):

    Enter a natural base:
    Enter the maximum number of digits:
    :bugeye: The program does not output 0100 when i = 4.
    Even more, the program outputs 0100 when it should output 1000.

    Why is this so??
    Last edited: Jan 12, 2006
  2. jcsd
  3. Jan 12, 2006 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Look at these two lines:


    bsq[k] = i / (int) Math.pow(base,k) % base;
    You're setting element k of the array, and then printing a different element. In other words, you're printing data that you calculated on the previous run, which is not at all what you intended.

    Instead, why don't you just reverse the way the for(k) loop runs? Use for(k = dgts-1; k >= 0; --k), and just print each value immediately after it's calculated. In fact, you don't even need to store it in an array.

    - Warren
  4. Jan 12, 2006 #3
    :smile: Thanks!

    Regarding the array--well, I intend to remove the System.out.println statements out of the 'for' loop and add (to this program) a method modifying a copy of the array...which, upon satisfying a certain condition, will be printed out.
    Actually, this is a simpler version of another program containing the same faulty 'for' loop...which I'll now fix :shy:

    I was writing a program that will calculate (via brute force) [itex]\{a_0,a_1,a_2,\ldots,a_n\}[/itex] with user-input values of p,q, and r (of course I'll name the variables in java differently). The goal is to calculate [itex]\{a_0,a_1,a_2,\ldots,a_n\}[/itex] according to my statement in this this thread, assuming it is true. So far, it's been over a month and no proof/disproof has developed for that statement :rolleyes: (also I'm quite busy and rarely have time for a genuine investigation :frown:).

    Here is the larger program, with the for-loop problem corrected:
    import java.io.*;

    public class TestMath1 {

    public static void main(String args[]) {

    int nmax = 0;

    double num = 0, den = 0,
    decm = 0, sum = 0;

    double[] bsq;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    try {
    System.out.println("Enter the numerator of the base:");
    num = Integer.parseInt(br.readLine ());

    System.out.println("Enter the denominator of the base:");
    den = Integer.parseInt(br.readLine ());

    System.out.println("Enter any natural number:");
    decm = Integer.parseInt(br.readLine ());
    catch(IOException e) {}

    nmax = (int) (Math.log(decm*den)/Math.log(num/den));
    bsq = new double[nmax+1];

    for(int i=0; i<Math.pow(num,nmax+1); i++) {

    for(int k=0; k<=nmax; k++) {
    bsq[k] = i / (int) Math.pow(num,k) % num;
    sum += bsq[k]/den * Math.pow(num/den, k);

    if(sum == decm/den) {

    for(int k=nmax; k>=0; --k) {
    System.out.print(bsq[k]/den + " ");

    System.out.println("\n\nWhich represents:\n");

    for(int k=nmax; k>=0; --k) {
    System.out.print((int)bsq[k] + "/" + (int)den + " ");



    sum = 0;



    Indeed, I shouldn't assign values in loops. Also, I will find a better value with which to initialize i...because the program consumes much more time with larger input values.

    Before you input values, remember that num > den and that num, den, and decm must be natural.
    Last edited: Jan 13, 2006
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook