Questions tagged [factors]

A number a is a factor of a number b if a divides b exactly. PLEASE USE [r-factor] TAG for questions on the factor data type in the _R_ language.

A number a is a factor of a number b if a divides b exactly; that is, if b % a = 0. A factor is also known as a divisor of a number.

Questions pertaining to finding factors of a number, i.e. factorization, typically use this tag.

This tag is also widely used for questions on the function factor or the "factor" data type in the scientific language for statistical computing and graphics, although using the tag is recommended.

619 questions
45
votes
13 answers

nᵗʰ ugly number

Numbers whose only prime factors are 2, 3, or 5 are called ugly numbers. Example: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... 1 can be considered as 2^0. I am working on finding nth ugly number. Note that these numbers are extremely sparsely…
Anil Katti
  • 1,313
  • 1
  • 14
  • 26
41
votes
7 answers

Why is my Swift loop failing with error "Can't form range with end < start"?

I have a for loop that checks if a number is a factor of a number, then checks if that factor is prime, and then it adds it to an array. Depending on the original number, I will get an error saying fatal error: Can't form range with end <…
lagoon
  • 6,417
  • 6
  • 23
  • 30
41
votes
6 answers

Segmented Sieve of Eratosthenes?

It's easy enough to make a simple sieve: for (int i=2; i<=N; i++){ if (sieve[i]==0){ cout << i << " is prime" << endl; for (int j = i; j<=N; j+=i){ sieve[j]=1; } } cout << i << " has " << sieve[i] << "…
John Smith
  • 11,678
  • 17
  • 46
  • 51
38
votes
2 answers

Why is the terminology of labels and levels in factors so weird?

An example of a non-settable function would be labels. You can only set factor labels when they are created with the factor() function. There is no labels<- function. Not that 'labels' and 'levels' in factors make any sense.... > fac <- factor(1:3,…
IRTFM
  • 258,963
  • 21
  • 364
  • 487
25
votes
5 answers

Mean by factor by level

Maybe this is simple but I can't find answer on web. I have problem with mean calculation by factors by level. My data looks typicaly: factor, value a,1 a,2 b,1 b,1 b,1 c,1 I want to get vector A contains mean only for level "a" If I type A on…
Bartek Taciak
  • 295
  • 1
  • 3
  • 6
24
votes
7 answers

Algorithm to find all the exact divisors of a given integer

I want to find all the exact divisors of a number. Currently I have this: { int n; int i=2; scanf("%d",&n); while(i<=n/2) { if(n%i==0) printf("%d,",i); i++; } getch(); } Is there any way to…
jairaj
  • 1,789
  • 5
  • 19
  • 32
23
votes
4 answers

Find the most frequent value by row

My problem is as follows: I have a data set containing several factor variables, which have the same categories. I need to find the category, which occurs most frequently for each row. In case of ties an arbitrary value can be chosen, although it…
ZMacarozzi
  • 697
  • 1
  • 5
  • 11
21
votes
3 answers

Remove unused factor levels from a ggplot bar plot

I want to do the opposite of this question, and sort of the opposite of this question, though that's about legends, not the plot itself. The other SO questions seem to be asking about how to keep unused factor levels. I'd actually like mine removed.…
Hendy
  • 10,182
  • 15
  • 65
  • 71
19
votes
4 answers

Enumerate factors of a number directly in ascending order without sorting?

Is there an efficient algorithm to enumerate the factors of a number n, in ascending order, without sorting? By “efficient” I mean: The algorithm avoids a brute-force search for divisors by starting with the prime-power factorization of n. The…
Todd Lehman
  • 2,880
  • 1
  • 26
  • 32
19
votes
7 answers

Recode categorical factor with N categories into N binary columns

Original data frame: v1 = sample(letters[1:3], 10, replace=TRUE) v2 = sample(letters[1:3], 10, replace=TRUE) df = data.frame(v1,v2) df v1 v2 1 b c 2 a a 3 c c 4 b a 5 c c 6 c b 7 a a 8 a b 9 a c 10 a b New data…
Keith Hughitt
  • 4,860
  • 5
  • 49
  • 54
16
votes
1 answer

R * not meaningful for factors ERROR

I have the following data.frame and I want to perform some calculations on the 2nd column. > test code age 1 101 15 2 102 25 3 103 16 4 104 u1 5 105 u1 6 106 u2 7 107 27 8 108 27 As you can see, the 2nd column does not…
mboon
  • 175
  • 1
  • 1
  • 6
16
votes
2 answers

Reshape data frame to convert factors into columns in R

I have a data frame where one particular column has a set of specific values (let's say, 1, 2, ..., 23). What I would like to do is to convert from this layout to the one, where the frame would have extra 23 (in this case) columns, each one…
Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31
16
votes
1 answer

What is the fastest algorithm to calculate all factors of an integer number?

I have written this block of code but it is consuming lots of time to calculate... Can you help me finding out an efficient way to do it? int tag; int* factors(int n) { int a[1000000]; for(int i=1;i<=n/2;i++) if(n%i==0) …
Biswajit
  • 339
  • 1
  • 4
  • 9
13
votes
3 answers

R machine learning packages to deal with factors with a large number of levels

I'm trying to do some machine learning stuff that involves a lot of factor-type variables (words, descriptions, times, basically non-numeric stuff). I usually rely on randomForest but it doesn't work w/factors that have >32 levels. Can anyone…
screechOwl
  • 27,310
  • 61
  • 158
  • 267
12
votes
14 answers

Finding factors of a given integer

I have something like this down: int f = 120; for(int ff = 1; ff <= f; ff++){ while (f % ff != 0){ } Is there anything wrong with my loop to find factors? I'm really confused as to the workings of for and while statements, so…
Wilson
  • 8,570
  • 20
  • 66
  • 101
1
2 3
41 42