# Frequency recursion

Page 1 of 1

## 13 Replies - 605 Views - Last Post: 23 March 2012 - 06:08 AMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=271961&amp;s=a9590bbbaf951f35ff89998f7fb25715&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jspak

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 21-March 12

# Frequency recursion

Posted 21 March 2012 - 03:50 PM

I'm having difficulty finding where to start on this project. My prof. wants us to write a static recursive method named frequencyCount(n,d) that returns the frequency of occurrence of a particular digit d in an integer n. For example, frequencyCount(1342457,4) returns 2, but frequencyCount (1342457,6) returns 0. We can't use local or static variables.
Is This A Good Question/Topic? 0

## Replies To: Frequency recursion

### #2 blackcompe

• D.I.C Lover

Reputation: 1009
• Posts: 2,186
• Joined: 05-May 05

## Re: Frequency recursion

Posted 21 March 2012 - 03:57 PM

What's the difficulty? In coding or logic? Surely, you can figure out an algorithm to count the number of occurrences. If it's a coding issue, lets see what you've tried so far.
Was This Post Helpful? 0

### #3 jspak

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 21-March 12

## Re: Frequency recursion

Posted 21 March 2012 - 04:04 PM

I haven't tried anything yet because I don't know where to start. I'm thinking that I have to make it so it stores the digits into an array and then compare the other number to the numbers in the array. I'm not sure how to do that though.
Was This Post Helpful? 0

### #4 blackcompe

• D.I.C Lover

Reputation: 1009
• Posts: 2,186
• Joined: 05-May 05

## Re: Frequency recursion

Posted 21 March 2012 - 04:12 PM

Ok, that's a start. Note that you could concatenate the integer with the empty string and use String.charAt and avoid the array altogether, or you could chop the integer down using division and modulus and examine each digit to avoid using strings. The former approach is easier.

So you got your algorithm. The only thing left to do is code... Are you in need of help with setting up a skeleton or a main method? Why don't you try and write something first. There are plenty of tutorials to help you get started. If you've got experience using streams, I don't think this should be all that hard.

This post has been edited by blackcompe: 21 March 2012 - 04:15 PM

Was This Post Helpful? 0

### #5 CasiOo

• D.I.C Lover

Reputation: 996
• Posts: 2,212
• Joined: 05-April 11

## Re: Frequency recursion

Posted 21 March 2012 - 04:22 PM

No local variables? why no local variables??
Was This Post Helpful? 0

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8032
• Posts: 31,185
• Joined: 06-March 08

## Re: Frequency recursion

Posted 21 March 2012 - 09:34 PM

The OP probably means instance variable
Was This Post Helpful? 0

### #7 blackcompe

• D.I.C Lover

Reputation: 1009
• Posts: 2,186
• Joined: 05-May 05

## Re: Frequency recursion

Posted 21 March 2012 - 10:57 PM

I didn't see the thing about restricting the use of variables. This would be a good challenge question. jspak: Use regular expressions.
Was This Post Helpful? 0

### #8 macosxnerd101

• Self-Trained Economist

Reputation: 9042
• Posts: 33,544
• Joined: 27-December 08

## Re: Frequency recursion

Posted 22 March 2012 - 06:47 AM

You shouldn't need any local variables. Big hint- x%10 returns the last digit of x; and x/10 divides x by 10, removing the end digit. Use that to evaluate each digit.
Was This Post Helpful? 2

### #9 blackcompe

• D.I.C Lover

Reputation: 1009
• Posts: 2,186
• Joined: 05-May 05

## Re: Frequency recursion

Posted 22 March 2012 - 01:48 PM

Quote

You shouldn't need any local variables. Big hint- x%10 returns the last digit of x; and x/10 divides x by 10, removing the end digit. Use that to evaluate each digit.

macosxnerd101: I'd like to see you do this without using strings... I'm totally calling you out. PM me if you come up with something.

This post has been edited by blackcompe: 22 March 2012 - 01:57 PM

Was This Post Helpful? 1

### #10 burakaltr

• D.I.C Head

Reputation: 86
• Posts: 212
• Joined: 07-November 10

## Re: Frequency recursion

Posted 22 March 2012 - 01:55 PM

The OP : You need a similar piece of code incorporated , like the one below to get each digit
``` onluk=0;
num2=num;
k=0;
for(int i1=0;i1<=19;i1++)
{
k=k+1;
dig[i1]=num % 10;
num=(num-dig[i1])/10;

}
```

Was This Post Helpful? 2

### #11 CasiOo

• D.I.C Lover

Reputation: 996
• Posts: 2,212
• Joined: 05-April 11

## Re: Frequency recursion

Posted 22 March 2012 - 06:17 PM

blackcompe, on 22 March 2012 - 01:48 PM, said:

Quote

You shouldn't need any local variables. Big hint- x%10 returns the last digit of x; and x/10 divides x by 10, removing the end digit. Use that to evaluate each digit.

macosxnerd101: I'd like to see you do this without using strings... I'm totally calling you out. PM me if you come up with something.

pm sent
Was This Post Helpful? 1

### #12 pbl

• There is nothing you can't do with a JTable

Reputation: 8032
• Posts: 31,185
• Joined: 06-March 08

## Re: Frequency recursion

Posted 22 March 2012 - 07:48 PM

burakaltr, on 22 March 2012 - 04:55 PM, said:

The OP : You need a similar piece of code incorporated , like the one below to get each digit
``` onluk=0;
num2=num;
k=0;
for(int i1=0;i1<=19;i1++)
{
k=k+1;
dig[i1]=num % 10;
num=(num-dig[i1])/10;

}
```

and what is the use of "k" in your loop ?
Was This Post Helpful? 0

### #13 burakaltr

• D.I.C Head

Reputation: 86
• Posts: 212
• Joined: 07-November 10

## Re: Frequency recursion

Posted 23 March 2012 - 04:24 AM

pbl, on 22 March 2012 - 07:48 PM, said:

burakaltr, on 22 March 2012 - 04:55 PM, said:

The OP : You need a similar piece of code incorporated , like the one below to get each digit
``` onluk=0;
num2=num;
k=0;
for(int i1=0;i1<=19;i1++)
{
k=k+1;
dig[i1]=num % 10;
num=(num-dig[i1])/10;

}
```

and what is the use of "k" in your loop ?

Because you'll later need it to keep track of number of digits.

```     for(int i1=0;i1<=19;i1++)
{
k=k+1;

dig[i1]=num % 10;
//if(dig[i1]<0){dig[i1]=dig[i1]+10;}
num=(num-dig[i1])/10;
if(num==0)break;

System.out.println(dig[i1]);
}
for(int i=0;i<k;i++){

if(b==dig[i])
y++;
//  System.out.println(dig[i]) 	;
System.out.println(y) 	;
// System.out.println(" freq of "+b+" = "+freqn[v]) 	;

}
//  System.out.println(" freq of "+b+" = "+freqn[v]) 	;

return y;
```

Was This Post Helpful? 1

### #14 macosxnerd101

• Self-Trained Economist

Reputation: 9042
• Posts: 33,544
• Joined: 27-December 08

## Re: Frequency recursion

Posted 23 March 2012 - 06:08 AM

@burakaltr: If the OP needs to use recursion, your solution would be invalid. That also is a cumbersome solution. To count the frequencies of all the digits, create an array of length 10. Then get the digit using the % operator, and increment count[digit].
Was This Post Helpful? 0

Page 1 of 1

 Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).